Post

Typescript 5

Typescript 5

third-party package

https://github.com/SBoudrias/Inquirer.js

1
npm install inquirer@9.1.4

Installing type definitions

1
npm install --save-dev @types/inquirer@9.0.3

See https://github.com/DefinitelyTyped/DefinitelyTyped for the details of the Definitely Typed project and the packages for which type declarations are available.

Persistently storing data

1
npm install lowdb@5.1.0

See https://github.com/typicode/lowdb for details if you want to use Lowdb in your projects.

The undefined type is used when variables are defined but have not been assigned a value

Table 3-1

When the typeof keyword is used on null values, the result is object. This is a long-standing behavior that dates back to the earliest days of JavaScript and that hasn’t been changed because so much code has been written that expects this behavior.

Table 4-1

You should use ECMAScript modules for most projects, just as long as they are supported by your JavaScript runtime, such as Node.js or your browser. Not only are ECMAScript modules the “real” standard, but an ECMAScript module can import from a CommonJS module, which means that you can mix and match module formats.

Creating the package.json file

1
npm init --yes

Adding packages using the Node Package Manager

1
2
npm install --save-dev typescript@5.0.2
npm install --save-dev tsc-watch@6.0.0

Compiling the TypeScript code

1
tsc

Running the compiled code

1
node dist/index.js

Table 5-1

Table 5-3

Table 5-4

Table 5-5

Displaying the list of files for compilation

1
tsc --listFiles

Starting the compiler in watch mode

1
tsc --watch

Starting the package command

1
npx tsc-watch --onsuccess "node dist/index.js"

See https://github.com/gilamran/tsc-watch for details of the other options provided by the ts-watch package.

The TypeScript compiler also provides an API that can be used to create custom tools, which can be useful if you need to integrate the compiler into a complex workflow. Microsoft doesn’t provide extensive documentation for the API, but there are some notes and examples at https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API.

Table 5-6

Table 5-7

Table 5-8

Table 5-9

See https://www.typescriptlang.org/docs/handbook/compiler-options.html for the complete set of options the compiler supports.

Table 5-10

Starting the Node.js debugger

1
node inspect dist/index.js

Starting Node.js in remote debugger mode

1
node --inspect-brk dist/index.js

The standard TypeScript linter used to be TSLint, but this has been deprecated in favor of the typescript-eslint package.

1
2
3
npm install --save-dev eslint@8.36.0
npm install --save-dev @typescript-eslint/parser@5.55.0
npm install --save-dev @typescript-eslint/eslint-plugin@5.55.0

Table 6-1

Adding packages to the project

1
2
3
npm install --save-dev jest@29.4.3
npm install --save-dev ts-jest@29.0.5
npm install --save-dev @types/jest@29.4.0

Table 6-2

Starting the unit test framework in watch mode

1
npx jest --watchAll

Table 7-1

Table 7-2

Using functions

Table 8-2

Table 13-3

This post is licensed under CC BY 4.0 by the author.