Using TypeScript with the ServiceNow SDK

asd

Add a TSConfig file

Create a tsconfig.json file that looks like this:

tsconfig.json
{
    "compilerOptions": {
       "outDir": "dist/module",
       "rootDir": "src",
       "allowJs": true,
   },
   "include": ["./src/**/*"]
 }

Install TypeScript

npm install typescript@latest --save-dev

This will create a package.json file and a package-Lock.json file in your project directory. The package.json file should look like this:

{
  "devDependencies": {
    "typescript": "^5.5.3" // Whatever TypeScript version is the latest
  }
}

Add devDependencies

Add the following devDependencies to your package.json file:

{
    // ...
    "devDependencies": {
        "@servicenow/eslint-plugin-sdk-app-plugin": "*",
        "@servicenow/glide": "^25.0.3",
        "eslint": "8.50.0",
        "typescript": "^5.5.3"
    }
}
 

Add a build script

Add a build script to your package.json file:

{
    // ...
    "scripts": {
        "build": "tsc --build --clean && tsc --build && now-sdk build"
    }
}

Compile your TypeScript files into JavaScript modules

npm run build

Deploy your application

now-sdk deploy

Note that at this point the now-sdk will output an overview of the files that were updated, but that this overview might be wrong. It was for me, as it consistently showed that 0 files were updated:

Printing most recent installation stats for x_comp_test:
 
        summary_complete_status: complete
        deployment_started: 2024-07-16 12:21:06
        deployment_finished: 2024-07-16 12:21:06
        added: 0
        updated: 0
        deleted: 0
        skipped: 0
        skipped_and_not_different: 0
        skipped_error: 0
        sys_id: 65676c7bdb130250553e8f8d13961990

References