TypeScript
Overview
TypeScript is a strongly typed programming language that builds on JavaScript by adding optional static types, then compiles down to plain JavaScript that runs anywhere JavaScript runs. It catches whole classes of errors before runtime and powers richer editor tooling for large-scale applications. Read this when configuring a TypeScript build, designing types, or debugging compiler errors.
Official sources
Install / setup
npm install typescript --save-dev
Core concepts
- Structural typing: types are compatible based on their shape (members), not their declared name; an object satisfying an interface's members is assignable to it.
- Type annotations and inference: annotate variables, parameters, and return types, but lean on the compiler's inference where it already knows the type.
- Interfaces and type aliases: model object shapes and unions;
interface is open to declaration merging, type can express unions, intersections, and mapped types.
- Generics: write reusable, type-safe components and functions parameterized over types (
Array<T>, Promise<T>).
- Union, intersection, and literal types: combine types and narrow them with control-flow analysis (type guards) to model precise states.
tsconfig.json: the project configuration file controlling the compiler — target, module system, strict flags, and which files are included.
- The
tsc compiler: type-checks and emits JavaScript; type errors do not necessarily block emit unless configured to.
Best practices
- Enable
strict mode in tsconfig.json — it turns on the full family of strict type-checking options recommended by the docs for catching more bugs.
- Prefer letting the compiler infer types for locals; annotate public API boundaries (function signatures, exported values) explicitly.
- Avoid
any; prefer unknown when a value's type is genuinely not known, then narrow it before use.
- Use union types and discriminated unions plus exhaustiveness checking instead of loose runtime type juggling.
Common pitfalls
- Treating TypeScript types as runtime values → types are erased at compile time; use runtime checks (
typeof, validation libraries) for runtime guarantees.
- Assuming a clean
tsc build means the JS won't run on errors → by default emit can still occur; use noEmitOnError if you want errors to block output.
- Overusing
any to silence errors → it disables checking and propagates; prefer unknown, generics, or proper narrowing.
Examples
interface User {
id: number;
name: string;
}
function greet(user: User): string {
return `Hello, ${user.name}`;
}
const u: User = { id: 1, name: "Ada" };
console.log(greet(u));
Further reading
Related skills
- ../javascript — the language TypeScript compiles to
1---2name: typescript3description: Typed superset of JavaScript that compiles to plain JS; consult for type system, tsconfig, and compiler usage.4domain: language5category: language6tags: [typescript, javascript, types, compiler, tsc]7official_sources:8 - https://www.typescriptlang.org/docs/9 - https://github.com/microsoft/TypeScript10 - https://www.typescriptlang.org/download11verified: 2026-06-1612metadata:13 source: https://skillmd.com/skills/gabriel7silva/typescript14---1516# TypeScript1718## Overview19TypeScript is a strongly typed programming language that builds on JavaScript by adding optional static types, then compiles down to plain JavaScript that runs anywhere JavaScript runs. It catches whole classes of errors before runtime and powers richer editor tooling for large-scale applications. Read this when configuring a TypeScript build, designing types, or debugging compiler errors.2021## Official sources22- Docs: https://www.typescriptlang.org/docs/23- Repo: https://github.com/microsoft/TypeScript24- Install / download: https://www.typescriptlang.org/download2526## Install / setup27```bash28npm install typescript --save-dev29```3031## Core concepts32- Structural typing: types are compatible based on their shape (members), not their declared name; an object satisfying an interface's members is assignable to it.33- Type annotations and inference: annotate variables, parameters, and return types, but lean on the compiler's inference where it already knows the type.34- Interfaces and type aliases: model object shapes and unions; `interface` is open to declaration merging, `type` can express unions, intersections, and mapped types.35- Generics: write reusable, type-safe components and functions parameterized over types (`Array<T>`, `Promise<T>`).36- Union, intersection, and literal types: combine types and narrow them with control-flow analysis (type guards) to model precise states.37- `tsconfig.json`: the project configuration file controlling the compiler — target, module system, `strict` flags, and which files are included.38- The `tsc` compiler: type-checks and emits JavaScript; type errors do not necessarily block emit unless configured to.3940## Best practices41- Enable `strict` mode in `tsconfig.json` — it turns on the full family of strict type-checking options recommended by the docs for catching more bugs.42- Prefer letting the compiler infer types for locals; annotate public API boundaries (function signatures, exported values) explicitly.43- Avoid `any`; prefer `unknown` when a value's type is genuinely not known, then narrow it before use.44- Use union types and discriminated unions plus exhaustiveness checking instead of loose runtime type juggling.4546## Common pitfalls47- Treating TypeScript types as runtime values → types are erased at compile time; use runtime checks (`typeof`, validation libraries) for runtime guarantees.48- Assuming a clean `tsc` build means the JS won't run on errors → by default emit can still occur; use `noEmitOnError` if you want errors to block output.49- Overusing `any` to silence errors → it disables checking and propagates; prefer `unknown`, generics, or proper narrowing.5051## Examples52```typescript53interface User {54 id: number;55 name: string;56}5758function greet(user: User): string {59 return `Hello, ${user.name}`;60}6162const u: User = { id: 1, name: "Ada" };63console.log(greet(u));64```6566## Further reading67- TypeScript Handbook: https://www.typescriptlang.org/docs/handbook/intro.html68- tsconfig reference: https://www.typescriptlang.org/tsconfig/6970## Related skills71- ../javascript — the language TypeScript compiles to