Introduction
A .ts file holds a program or module written in
TypeScript, Microsoft’s language that adds
static types to JavaScript. The TypeScript compiler
checks the types and writes plain JavaScript, which browsers and other runtimes then
run. Deno and Bun run TypeScript files directly, and recent versions of Node.js can too
by stripping the types.
Related extensions: .tsx for TypeScript with JSX markup, as used with
React; .mts and .cts for ECMAScript and CommonJS modules; and
.d.ts for declaration files, which describe the types of a JavaScript
library and hold no code. The same .ts extension is used for MPEG
transport stream video.
The pages listed below are a complete reference to the language as of TypeScript 7.0. They concentrate on what TypeScript adds; the language it extends is described on the JavaScript pages. They follow the TypeScript Handbook and the release notes, and the examples were checked against the 7.0 compiler.
Structure
A TypeScript file is plain text, normally UTF-8, with no header. Its syntax is
JavaScript’s with additions for types: annotations after names, such as
count: number, and declarations such as interface and
type. Compiling removes nearly all of these, so the output has the same shape
as the source. A few forms, such as enum, generate code. How a project is
compiled is set in a tsconfig.json file beside the sources.
interface User {
name: string;
age?: number;
}
export function greet(user: User): string {
return `Hello, ${user.name}`;
}
The File structure page covers the extensions, encoding, scripts and modules, directives and what is emitted.
Language reference
The reference is split into these pages.
- File structure – the extensions and how each is treated, scripts and modules, triple-slash and comment directives,
tsconfig.json, type erasure and emit - Lexical structure – contextual keywords, line-break rules, type arguments versus comparisons, and the differences in
.tsxfiles - Types – primitive types,
any,unknown,neverandvoid, object types, arrays, tuples, literal types, structural typing and assignability - Type operators – union, intersection,
keyof,typeof, indexed access and template literal types - Generics – type parameters, constraints, defaults, variance annotations,
consttype parameters and inference - Conditional and mapped types – conditional types,
infer, distribution, mapped types, key remapping and the utility types - Narrowing – control-flow analysis, type guards, predicates, assertion functions, discriminated unions,
satisfies,asandas const - Functions – signatures, overloads,
thisparameters, optional and rest parameters - Classes – parameter properties, access modifiers,
abstract,override,implementsand index signatures - Interfaces and aliases – declaration merging,
extends, and interface versus type - Enums – numeric, string,
constand ambient enums, and what they emit - Decorators – the standard and legacy forms, and metadata
- Modules and namespaces –
import type,export =, module formats and resolution, namespaces and ambient modules - Declaration files –
.d.ts,declare, augmentation, publishing and DefinitelyTyped - JSX –
.tsxfiles, the emit modes and theJSXnamespace - Compiler options – the strictness and emit options that change the meaning of a file, and what changed in 6.0 and 7.0
- Versions – every release from 1.0 in 2014 to 7.0 in 2026, with its main language features
Not described
Known, not yet covered
- Reference:
usingandawait usingdeclarations (explicit resource management,Symbol.dispose) are only named on the lexical structure and versions pages; how they work is not described. Comment directives (// @ts-ignore,// @ts-expect-error,// @ts-nocheck) and JSDoc types in JavaScript files checked withcheckJsare not described either. - Reference: the compiler options page does not cover
tsconfig.jsonextends, the build information file (.tsbuildinfo), the language service andtsserver, or the typings of the standard library (lib.dom.d.ts,lib.es*.d.ts) beyond the utility types. How Node.js runs TypeScript by stripping types is not described. - Viewer: the contextual words are read from what follows them:
typeis a keyword in a type alias,import type { A },import type A from,export type { A },export type * fromand the{ type B }modifier;using(also inawait usingandfor (using x of y)) before the name it declares; andisbefore a name, as in the type predicatex is string. Each stays plain as an ordinary name (import type from "x"names a default import). - Viewer: JSX in a
.tsxfile is not understood: in<div className="x">{y}</div>the tag, attribute and text are plain apart from the strings. - Viewer: only the built-in type names and the common utility types (
Partial,Record,ReturnType) are coloured as types. An interface, class, type alias or type parameter you declare (I,T) is plain, and a call is coloured as a function by name only. - Viewer: the file is not parsed or type-checked, so there is no outline, no type errors and no list of problems, and only the first 1 MiB is read and coloured. A
.tsfile that is really an MPEG transport stream is told apart by its content (see the other formats that use.ts).
Also used for
- MPEG transport stream – The broadcast TV container: 188-byte packets tagged with stream IDs.
- MPEG transport stream (Blu-ray and AVCHD) – The broadcast TV container: 188-byte packets tagged with stream IDs.
Links
Software
- TypeScript – the language’s own compiler,
tsc, which checks types and writes JavaScript (Windows, macOS, Linux) - Visual Studio Code – free code editor with built-in TypeScript support (Windows, macOS, Linux)
- WebStorm – JetBrains’ development environment for JavaScript and TypeScript (Windows, macOS, Linux)
- Deno – runtime that runs TypeScript directly (Windows, macOS, Linux)
- Bun – runtime that runs TypeScript directly (Windows, macOS, Linux)
- Node.js – JavaScript runtime; recent versions run TypeScript by stripping the types (Windows, macOS, Linux)
Developers
- typescript – the compiler as a library, with an API for parsing and checking (JavaScript)
- esbuild – strips types and bundles TypeScript (Go)
- SWC – a fast TypeScript and JavaScript compiler (Rust)
Reference
- The TypeScript Handbook – the official guide to the language
- TSConfig reference – every compiler option
- TypeScript roadmap – the features of each release