TypeScript
TypeScript is a superset of JavaScript that adds optional static typing, enabling robust tools for large-scale application development across any browser, host, or OS. The current stable version is 6.0.3. New stable versions are typically released every few months, preceded by beta and release candidate versions, ensuring a predictable upgrade path.
Common errors
-
TS2307: Cannot find module '...' or its corresponding type declarations.
cause The TypeScript compiler cannot locate the module you are trying to import, or it's missing type definition files (`.d.ts`) for a JavaScript library.fixFor third-party JavaScript libraries, install their type definitions (e.g., `npm install -D @types/library-name`). Ensure your `tsconfig.json` `moduleResolution` is correctly configured (e.g., `"node"` or `"bundler"`). -
TS2339: Property 'X' does not exist on type 'Y'.
cause You are attempting to access a property (`X`) that is not declared on the type (`Y`) of the object.fixVerify that the property truly exists on the object. If it's a known property from a less-typed source, use a type assertion (e.g., `(myObject as any).X`) or add a type guard to narrow the type (e.g., `if ('X' in myObject)`). -
TS2554: Expected X arguments, but got Y.
cause A function or method is being called with an incorrect number of arguments, not matching its defined signature.fixReview the function's signature and provide the correct number of arguments. If some arguments are optional, ensure they are handled correctly. -
TS1206: Decorators are not supported if 'experimentalDecorators' is set to 'false'.
cause You are using JavaScript decorators (Stage 3 proposal) in your code without enabling the `experimentalDecorators` compiler option.fixSet `"experimentalDecorators": true` within the `compilerOptions` section of your `tsconfig.json` file. Note that these are experimental and may change in future TypeScript versions.
Warnings
- breaking Major versions of TypeScript can introduce breaking changes to the language, compiler options, or API. Always consult the release notes before upgrading.
- gotcha Installing TypeScript globally (`npm install -g typescript`) can lead to version mismatches with project-local TypeScript installations, causing unexpected compilation behavior.
- gotcha TypeScript's default configuration in `tsconfig.json` can be permissive. For robust type checking and to catch common errors, strict mode is highly recommended.
- breaking TypeScript requires a minimum Node.js version of 14.17.0. Using older Node.js versions may result in installation errors or unexpected compiler behavior.
Install
-
npm install typescript -
yarn add typescript -
pnpm add typescript
Imports
- ts
import * as ts from 'typescript';
Quickstart
npm install -D typescript
// Create a file named 'hello.ts'
// console.log(`
// function greet(name: string) {
// console.log(`Hello, ${name}!`);
// }
//
// greet("World");
// greet(123); // This will cause a TypeScript error
// `);
// Compile the TypeScript file
npx tsc hello.ts
// Run the compiled JavaScript file
node hello.js