[EN] Working with TypeScript in NetSuite
Configuring all environment
TypeScript is a powerful superset of JavaScript that brings static typing and advanced features to the language. As a developer working with NetSuite, leveraging TypeScript can enhance your productivity, maintainability, and code quality. In this article, we will explore how to set up TypeScript in NetSuite and configure the tsconfig file to use the AMD module system and the ES2015 module output the source to match with NetSuite requeriments.
Prerequisites: Before getting started, ensure you have the following prerequisites in place:
-
SuiteCloud SDK installed on your local machine (includes Suite CLI and JDK17)
-
Basic knowledge of TypeScript and JavaScript
Setting Up TypeScript in NetSuite: To work with TypeScript in NetSuite, you need to set up your development environment appropriately. Follow these steps to get started:
Step 1: Create a New SuiteScript Project Using Suite CLI, create a new SuiteScript project in your desired directory. Open your terminal or command prompt and run the following command:
suitecloud project:create
Follow the prompts to choose the project type, project name, and other relevant details.
Step 2: Install TypeScript Next, install TypeScript as a dev dependency in your project:
npm install typescript --save-dev
npm install @hitc --save-dev
Step 3: Initialize TypeScript Configuration Create a new file named tsconfig.json in the root of your project. This file will hold TypeScript configuration settings for your NetSuite project.
Configuring tsconfig.json for AMD Module and ES2015 OutDir: The tsconfig.json file allows you to customize TypeScript's behavior. In this section, we'll focus on configuring it for AMD module and setting the ES2015 module output directory (OutDir).
{
"compilerOptions": {
"target": "es5",
"module": "amd",
"lib": [
"es7",
"es2015.promise",
"dom"
],
"sourceMap": false,
"newLine": "LF",
"outDir": "./src/FileCabinet/SuiteScripts/<Name of your Project>",
"strict": false,
"alwaysStrict": false,
"moduleResolution": "node",
"baseUrl": ".",
"experimentalDecorators": true,
"skipLibCheck": true,
"paths": {
"N": [
"node_modules/@hitc/netsuite-types/N"
],
"N/*": [
"node_modules/@hitc/netsuite-types/N/*"
]
}
}
}
Let’s explain the important options:
- "target": "es5": Sets the ECMAScript version that TypeScript will compile to. NetSuite currently supports ES5, so we choose this version to ensure compatibility.
- "module": "amd": Configures TypeScript to use the AMD module system. NetSuite uses AMD modules to manage dependencies.
- "outDir: Specifies the output directory where compiled JavaScript files will be generated. We use "./src/FileCabinet/Suitescripts/" as the output directory in this example, but you can customize it as per your project structure.
- "strict": true: Enables strict type-checking options for a more robust codebase.
- "esModuleInterop": true: Enables interoperability between CommonJS and ES Modules, facilitating seamless integration with NetSuite's AMD modules.
- "skipLibCheck": true: Skips type checking of declaration files, which can speed up the compilation process.
Integrating TypeScript into your NetSuite projects can significantly improve your development experience and code quality. By configuring the tsconfig.json file with AMD module and ES2015 OutDir settings, you ensure compatibility with NetSuite’s module system and organize your compiled files efficiently.
As you build complex SuiteScript applications, TypeScript’s static typing will prove invaluable in catching errors early and providing enhanced code assistance. Embrace the power of TypeScript to unlock the full potential of your NetSuite development process and deliver more reliable and maintainable solutions. Happy coding!