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.
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 all of these, so the JavaScript output has the same
shape as the source. 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}`;
}
Also used for
Links
Software
- TypeScript, with the
tsccompiler (Windows, macOS, Linux) - Visual Studio Code (Windows, macOS, Linux)
- WebStorm (Windows, macOS, Linux)
- Deno (Windows, macOS, Linux)
- Bun (Windows, macOS, Linux)
- Node.js (Windows, macOS, Linux)
Developers
- typescript, the compiler as a JavaScript library, with an API for parsing and checking
- esbuild, Go, strips types and bundles TypeScript
- SWC, Rust, a fast TypeScript and JavaScript compiler