What is TypeScript?
TypeScript is an open-source language maintained and developed by Microsoft. TypeScript adds additional syntax to JavaScript to support a tighter integration with your editor. Catch errors early in your editor or in your CI/CD pipeline, and write more maintainable code.First TypeScript code
Take a look at this code snippet:type keyword) is responsible for declaring our custom object type representing users. Later we utilize this newly created type to create the function isAdult that accepts one argument of type User and returns a boolean. After this, we create justine, our example data that can be used for calling the previously defined function. Finally, we create a new variable with information on whether justine is an adult.
There are additional things about this example that you should know:
- If we do not comply with the declared types, TypeScript will inform us that something is wrong and prevent misuse.
- Not everything must be typed explicitly — TypeScript infers types for us. For example, the variable
isJustineAnAdultis of typebooleaneven if we didn’t type it explicitly, andjustinewould be a valid argument for our function even though we didn’t declare this variable as ofUsertype.
What does TypeScript consist of?
TypeScript consists of two main components: the code itself and type definitions.TypeScript code
The code part is regular JavaScript with additional TypeScript-specific syntax for type annotations. When TypeScript code is compiled, all the TypeScript-specific parts are removed, resulting in clean JavaScript that can run in any environment. For example:Type definitions
Type definitions describe the shape of existing JavaScript code. They are usually stored in.d.ts files and don’t contain any actual implementation — they only describe the types. These definitions are essential for interoperability with JavaScript: code is not usually distributed as TypeScript, but instead transpiled to JavaScript that includes sidecar type definition files.
For example, when you use Node.js with TypeScript, you’ll need type definitions for Node.js APIs. This is available via @types/node. Install it using:
fs.readFile or http.createServer. For example:
@types namespace, maintained by the DefinitelyTyped community. This enables seamless integration of existing JavaScript libraries with TypeScript projects.
Transform capabilities
TypeScript also includes powerful transformation capabilities, particularly for JSX (used in React and similar frameworks). The TypeScript compiler can transform JSX syntax into regular JavaScript, similar to how Babel works.While we won’t cover these transformation features in these articles, it’s worth noting that TypeScript isn’t only a tool for type checking — it’s also a build tool for transforming modern JavaScript syntax into compatible versions for different environments.
How to run TypeScript code
There are a few possible ways to run TypeScript code:Run natively
Use Node.js’s built-in TypeScript support (v22.7.0+) to run
.ts files directly without any tools.Use a runner
Use tools like
ts-node or tsx for more advanced processing or older Node.js versions.Transpile
Compile TypeScript to JavaScript using
tsc for full control over the build output.Publish a package
Learn how to publish TypeScript packages to npm correctly.