superset of JavaScript. Every valid .js file is already valid TypeScript, you layer types on top incrementally.
-
TypeScript compiles down to plain JavaScript, so the runtime (browser or Node.js) never sees a
.tsfile. -
It adds a
compilestep between writing code and running it. That step is where all type errors are surfaced. - It does not change JavaScript’s runtime behavior.
-
The types exist only at compile time, they are completely
erasedin the emitted JavaScript. They have zeroruntimecost.
TypeCheck.ts
Checking JavaScript with tsc
You do not have to migrate to.ts files to get TypeScript’s benefits.
--checkJsenables type checking in.jsfiles.--noEmitonly performs type-checking, does not output any compiled.jsfiles.--strictenables all strict type-checking options (recommended).- It is a shorthand that enables
strictNullChecks,noImplicitAny,strictFunctionTypes, and several others at once.
- It is a shorthand that enables
[!WARNING]watchmodewatchmode recompiles on file changes, but it only watches.tsfiles.
- With a
tsconfig.jsonpresent, just runtsc -wfrom the project root to watch all files.
tsx
tsx is a fast TypeScript runner built on esbuild. It runs .ts and .tsx files directly without a separate compile step.
- Useful for scripts, CLI tools, and quick prototyping.
tsconfig.json
Thetsconfig.json file controls how TypeScript compiles your project.
- You can create it manually or run
tsc --initto generate a default one.
Expandable tsconfig.json
- You can learn about all the options in the TypeScript Handbook, or check this cheatsheet on Total Typescript.
Type Annotations
You annotate variables, parameters, and return types with: type.
- TypeScript can also infer types, you don’t always have to annotate.
Literal Types
Instead of a broad type likestring, a literal type restricts a value to one specific value.
- These are especially useful in function parameters to restrict allowed values.
Strict Null Checks
WithoutstrictNullChecks, null and undefined are assignable to any type, a common source of runtime bugs.
- With it enabled, you must explicitly handle
nullandundefined.
- To allow
null, use a union type:
Union Types
Aunion type means a value can be one of several types.
Type Narrowing and Type Guards
TypeScript tracks types throughconditional branches, this is called narrowing.
- A
type guardis any expression that narrows the type in a scope.
Type Aliases and Interfaces
Both let you name a type, but they have different strengths.[!WARNING] Usage Use a type alias for primitives and interface for object shapes, but this is a convention, not a rule.
- Type aliases can represent any type, including
unionsandprimitives, while interfaces are primarily forobjectshapes.
- Interfaces can
extendother interfaces, while type aliases useintersections.
- Declaration merging interfaces with the same name are merged automatically.
Promises
Promises in TypeScript are generic, you declare what they resolve to.[!WARNING] Return Types TypeScript will infer the return type in most cases, but being explicit is useful.
Generics
Generics let you write reusable code that works with any type while preserving type safety.- You can constrain generics with
extends.
Utility Types
TypeScript ships built-in generic types for common transformations.Readonly<T>makes all properties immutable.
Partial<T>makes all properties optional, useful for updatepatch operations.
Pick<T, K>constructs a type with only the specified keys.
Omit<T, K>is the opposite ofPick, builds a type without the specified keys.
any & unknown
any opts a value completely out of the type system. Use it as a last resort.
-
If you find yourself reaching for
any, considerunknownfirst.- Both can hold any value, but
unknownis safer, you must narrow it before using it.
- Both can hold any value, but
Index Access Types
Extract the type of a property using bracket notation on a type.Type Assertions
Type assertions tell the compiler to treat a value as a specific type, usingas.
[!WARNING] Unsafe Assertions
as does not perform runtime checks. If you’re wrong, you get a runtime error, not a compile error. Use type guards when possible.
DefinitelyTyped
Many npm packages are written in JavaScript and don’t include types. The community publishes type definitions under the@types scope on npm.
- These come from the DefinitelyTyped repository.
Zod
Zod is a schema declaration and validation library. It bridges the gap between TypeScript’s static types (compile-time) and runtime data validation.- TypeScript types are erased at runtime, they can’t validate an API response.
- Zod schemas validate at runtime and infer the TypeScript type automatically.