Quick reference

TypeScript reference

Types, narrowing, generics and compiler settings for safer JavaScript.

TypeScript adds a static type layer to JavaScript. Its value comes from narrowing and from modelling data honestly at boundaries; types disappear at runtime, so runtime validation is still required for untrusted input.

Read the full explanationJavaScript Foundations

Work through the guided lesson, then test yourself with a challenge.

Open course
176 entries

Backwards Compatibility

Classes and modules

Command-line Options

Compiler and project settings

Compiler Diagnostics

Compiler options

Completeness

Editor Support

Emit

declaration

"declaration": true

Generate .d.ts files from TypeScript and JavaScript files in your project.

Open entry

declarationDir

"declarationDir": "value"

Specify the output directory for generated declaration files.

Open entry

declarationMap

"declarationMap": true

Create sourcemaps for d.ts files.

Open entry

downlevelIteration

"downlevelIteration": true

Emit more compliant, but verbose and less performant JavaScript for iteration.

Open entry

emitBOM

"emitBOM": true

Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files.

Open entry

emitDeclarationOnly

"emitDeclarationOnly": true

Only output d.ts files and not JavaScript files.

Open entry

importHelpers

"importHelpers": true

Allow importing helper functions from tslib once per project, instead of including them per-file.

Open entry

inlineSourceMap

"inlineSourceMap": true

Include sourcemap files inside the emitted JavaScript.

Open entry

inlineSources

"inlineSources": true

Include source code in the sourcemaps inside the emitted JavaScript.

Open entry

mapRoot

"mapRoot": "value"

Specify the location where debugger should locate map files instead of generated locations.

Open entry

newLine

"newLine": "crlf"

Set the newline character for emitting files.

Open entry

noEmitHelpers

"noEmitHelpers": true

Disable generating custom helper functions like '__extends' in compiled output.

Open entry

noEmitOnError

"noEmitOnError": true

Disable emitting files if any type checking errors are reported.

Open entry

outDir

"outDir": "value"

Specify an output folder for all emitted files.

Open entry

outFile

"outFile": "value"

Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output.

Open entry

preserveConstEnums

"preserveConstEnums": true

Disable erasing 'const enum' declarations in generated code.

Open entry

removeComments

"removeComments": true

Disable emitting comments.

Open entry

sourceMap

"sourceMap": true

Create source map files for emitted JavaScript files.

Open entry

sourceRoot

"sourceRoot": "value"

Specify the root path for debuggers to find the reference source code.

Open entry

stripInternal

"stripInternal": true

Disable emitting declarations that have '@internal' in their JSDoc comments.

Open entry

Generics and utility types

as const

["a", "b"] as const

Makes literals readonly and narrow instead of widening to string[].

Open entry

Awaited<T>

Awaited<Promise<User>>

Unwraps the value type of a promise, recursively.

Open entry

Generic constraint

<T extends { id: string }>

Restricts a type parameter so the function can rely on certain members.

Open entry

Generic function

function first<T>(items: T[]): T

A type parameter makes a function work with many types while keeping the relationship between inputs and output.

Open entry

Indexed access

T[K]

Looks up the type of a property by key. Combine it with keyof for type-safe helpers.

Open entry

keyof

keyof T

The union of an object type’s property names. Useful for typed property access.

Open entry

Parameters<T>

Parameters<typeof fn>

A tuple of a function’s parameter types.

Open entry

Partial<T>

Partial<User>

Makes every property optional. Ideal for patch and update payloads.

Open entry

Pick<T, K> / Omit<T, K>

Pick<User, "id">

Builds a new object type from selected or excluded keys.

Open entry

Readonly<T>

Readonly<User>

Makes all top-level properties readonly. It is shallow, so nested objects stay mutable.

Open entry

Record<K, V>

Record<"a" | "b", number>

Maps a union of keys to a value type. All keys are required by default.

Open entry

Required<T>

Required<User>

Makes every property required, the inverse of Partial.

Open entry

ReturnType<T>

ReturnType<typeof fn>

Extracts the return type of a function type. Handy when you cannot import the type directly.

Open entry

satisfies

config satisfies Config

Checks a value against a type without widening it, so literal details are preserved.

Open entry

Template literal type

`${string}-${string}`

Builds string types from patterns. Useful for event names and keys.

Open entry

Interop Constraints

JavaScript Support

Language and Environment

Modules

allowArbitraryExtensions

"allowArbitraryExtensions": true

Enable importing files with any extension, provided a declaration file is present.

Open entry

allowImportingTsExtensions

"allowImportingTsExtensions": true

Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set.

Open entry

allowUmdGlobalAccess

"allowUmdGlobalAccess": true

Allow accessing UMD globals from modules.

Open entry

baseUrl

"baseUrl": "value"

Specify the base directory to resolve non-relative module names.

Open entry

customConditions

"customConditions": []

Conditions to set in addition to the resolver-specific defaults when resolving imports.

Open entry

module

"module": "none"

Specify what module code is generated.

Open entry

moduleSuffixes

"moduleSuffixes": []

List of file name suffixes to search when resolving a module.

Open entry

noResolve

"noResolve": true

Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project.

Open entry

noUncheckedSideEffectImports

"noUncheckedSideEffectImports": true

Check side effect imports.

Open entry

resolveJsonModule

"resolveJsonModule": true

Enable importing .json files.

Open entry

resolvePackageJsonExports

"resolvePackageJsonExports": true

Use the package.json 'exports' field when resolving package imports.

Open entry

resolvePackageJsonImports

"resolvePackageJsonImports": true

Use the package.json 'imports' field when resolving imports.

Open entry

rewriteRelativeImportExtensions

"rewriteRelativeImportExtensions": true

Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files.

Open entry

rootDir

"rootDir": "value"

Specify the root folder within your source files.

Open entry

rootDirs

"rootDirs": []

Allow multiple folders to be treated as one when resolving modules.

Open entry

typeRoots

"typeRoots": []

Specify multiple folders that act like './node_modules/@types'.

Open entry

types

"types": []

Specify type package names to be included without being referenced in a source file.

Open entry

Narrowing and unions

Output Formatting

Projects

Type Checking

allowUnreachableCode

"allowUnreachableCode": true

Disable error reporting for unreachable code.

Open entry

allowUnusedLabels

"allowUnusedLabels": true

Disable error reporting for unused labels.

Open entry

alwaysStrict

"alwaysStrict": true

Ensure 'use strict' is always emitted.

Open entry

noFallthroughCasesInSwitch

"noFallthroughCasesInSwitch": true

Enable error reporting for fallthrough cases in switch statements.

Open entry

noImplicitAny

"noImplicitAny": true

Enable error reporting for expressions and declarations with an implied 'any' type.

Open entry

noImplicitOverride

"noImplicitOverride": true

Ensure overriding members in derived classes are marked with an override modifier.

Open entry

noImplicitReturns

"noImplicitReturns": true

Enable error reporting for codepaths that do not explicitly return in a function.

Open entry

noImplicitThis

"noImplicitThis": true

Enable error reporting when 'this' is given the type 'any'.

Open entry

noPropertyAccessFromIndexSignature

"noPropertyAccessFromIndexSignature": true

Enforces using indexed accessors for keys declared using an indexed type.

Open entry

noUnusedLocals

"noUnusedLocals": true

Enable error reporting when local variables aren't read.

Open entry

noUnusedParameters

"noUnusedParameters": true

Raise an error when a function parameter isn't read.

Open entry

strictBindCallApply

"strictBindCallApply": true

Check that the arguments for 'bind', 'call', and 'apply' methods match the original function.

Open entry

strictBuiltinIteratorReturn

"strictBuiltinIteratorReturn": true

Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'.

Open entry

strictFunctionTypes

"strictFunctionTypes": true

When assigning functions, check to ensure parameters and the return values are subtype-compatible.

Open entry

strictNullChecks

"strictNullChecks": true

When type checking, take into account 'null' and 'undefined'.

Open entry

strictPropertyInitialization

"strictPropertyInitialization": true

Check for class properties that are declared but not set in the constructor.

Open entry

useUnknownInCatchVariables

"useUnknownInCatchVariables": true

Default catch clause variables as 'unknown' instead of 'any'.

Open entry

Types and shapes

any

value: any

Disables type checking. Treat it as an escape hatch that should not cross module boundaries.

Open entry

Array types

string[] / Array<string>

Declares an array of a single element type. Readonly<string[]> prevents mutation.

Open entry

enums

enum Role { Admin, Learner }

Named constants. Prefer union literals for new code because enums add runtime objects.

Open entry

Function type

(a: number) => string

Describes a function signature, including parameters and return type.

Open entry

Interface

interface User { … }

Describes an object shape. Interfaces can be extended and declared twice; type aliases cannot.

Open entry

Literal type

type Direction = "up" | "down"

Restricts a value to exact literals. Perfect for options, states and event names.

Open entry

never

never

The type of a value that cannot happen, or of a function that never returns normally.

Open entry

null and undefined

string | null

With strictNullChecks, null and undefined are separate types and must be handled explicitly.

Open entry

Optional property

name?: string

The property may be missing or undefined. Do not use it for values that are always present but sometimes empty.

Open entry

Readonly

readonly id: string

Prevents reassignment of the property after creation. It is shallow and checked only by the compiler.

Open entry

Record

Record<string, number>

An object whose keys and values share types.

Open entry

Tuple

[string, number]

A fixed-length array where each position has a type. Optional and rest elements are allowed.

Open entry

Type alias

type User = { id: string }

Gives a name to any type, including unions and tuples. Aliases are erased at build time.

Open entry

Type annotation

const name: string = "Ada";

Declares the expected type of a value. Inference usually means you only annotate boundaries.

Open entry

unknown

value: unknown

A safe alternative to any: you must narrow before using the value.

Open entry

void

function log(): void

The return type for functions that do not return a value. A void callback may ignore any return value.

Open entry

Watch and Build Modes

Keep going

Turn the reference into recall.

Read the full lesson, then solve the challenge without looking. That is where the knowledge sticks.