@btonasse/suitescript-types
v1.0.2
Published
Typings for SuiteScript 2.1
Downloads
369
Readme
SuiteScript 2.1 Typings
This is a fork of the awesome @hitc/netsuite-types
. The main difference is that the purpose of this library is to provide typings for JS files - not TypeScript - and without needing to change anything in how you write your suitescripts.
This means you can keep writing vanilla JS and enjoy full Intellisense/autocompletion on:
- The imported module instance objects (as long as they're valid
N/
modules) - The entry point context objects
- Global modules like
N/log
andN/util
Installation Instructions
You can either add this package as a library to your IDE or as a dev dependency directly to your project.
As a project dependency
npm install --save-dev @btonasse/suitescript-types
Once installed, create a file called tsconfig.json
in your project root and have these options configured:
{
"compilerOptions": {
"module": "ES6",
"allowJs": true,
"checkJs": true,
"noEmit": true,
"typeRoots": ["./node_modules/@btonasse/suitescript-types"],
"strict": true
},
"include": ["src/**/*.js"],
"exclude": ["node_modules"]
}
You can download the latest published typings library at any time by simply running the install command again.
IDE set-up (WebStorm)
- Clone this repo to a local folder
- Go to
Settings -> Languages & Frameworks -> JavaScript -> Libraries
- Click
Add...
and select the local folder
:warning: If you're using Oracle's SuiteCloud Development Framework plugin, you need to disable the SuiteScript1.0 and SuiteScript2.0 modules that are automatically added to new projects under the
.idea
folder, otherwise they will shadow this package's definitions.
Usage
Callback function and entry points
To get Intellisense/autocompletion, you can structure your callback function in two different ways:
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(["N/record", "N/search"], (record, search) => {
return {
beforeLoad: (scriptContext) => {
// entry point implementation
},
beforeSubmit: (scriptContext) => {
// entry point implementation
},
};
});
Or
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(["N/record", "N/search"], (record, search) => {
/** @type {import("N/entryPoints").UserEvent.beforeLoad} */
const beforeLoad = (scriptContext) => {
// entry point implementation
};
/** @type {import("N/entryPoints").UserEvent.beforeSubmit} */
const beforeSubmit = (scriptContext) => {
// entry point implementation
};
return {
beforeLoad: beforeLoad,
beforeSubmit: beforeSubmit,
};
});
JSDoc types
Usually your IDE will pick-up the correct type in a JSDoc annotation. However, for certain interfaces like Record
and Sublist
, there can be conflicts with either built-in interfaces or other N
modules. For example:
/**
* {Record} currentRecord
*/
const myFunc = (currentRecord) => {
// What is the type of currentRecord?
};
In the example above the IDE might not infer the type correctly (are we're referring to N/record
, N/workbook
or even the typescript built-in with the same name?). To solve this, use import types as per the official TypeScript documentation
/**
* {import("N/record").Record} currentRecord
*/
const myFunc = (currentRecord) => {
// Now we know we're talking about N/record!
};