Skip to content
On this page

Using TypeScript

AirCode supports TypeScript. You can directly use TypeScript in the AirCode environment.

TypeScript provides convenience in the following scenarios:

  1. Static type checking, which helps improve the robustness and maintainability of your code.
  2. Support for the latest ECMAScript features while also being compatible with JavaScript.
  3. Provides object-oriented programming capabilities such as classes, interfaces, and modules.
  4. And more...

Create a New TypeScript Application

When creating a new application, you can choose to use TypeScript as the default language. Here's how:

  1. Click to create a new application.
  2. In the pop-up window, check the box for TypeScript:
  1. Click confirm, and the default function code for your new application will use TypeScript:

Enable TypeScript for Existing JavaScript Applications

If you have an existing JavaScript application, you can also enable TypeScript. Follow these steps:

  1. Install the TypeScript dependency:
  1. Create a tsconfig.json file (the system will automatically fill in the best practices for configuration, generally no secondary modifications are needed):
  1. Create your TypeScript function, then debug or deploy. In the console, you can see the logs indicating whether the TypeScript compilation was successful or failed:

Importing ES Module Modules in TypeScript

AirCode supports importing ECMAScript Modules(ESM) in TypeScript. To import ESM, you need to modify your tsconfig.json configuration by changing moduleResolution to "NodeNext". Then, change the file extension of your cloud function file from .ts to .mts.

json
{
  ...
  "compilerOptions": {
    ...
    "moduleResolution": "NodeNext"
  }
}
{
  ...
  "compilerOptions": {
    ...
    "moduleResolution": "NodeNext"
  }
}