What Is a .TS File?
TypeScript source
The .ts extension is also used by MPEG transport stream.Not sure which you have?
TypeScript Source File (.ts)
What is a TypeScript file?
A TypeScript (.ts) file is a source code file written in TypeScript, a strongly typed programming language developed by Microsoft that builds on JavaScript by adding static type definitions. TypeScript is a superset of JavaScript, meaning any valid JavaScript code is also valid TypeScript code. TypeScript files are compiled (transpiled) to JavaScript before execution.
The extension has one well-known collision: .ts is also the extension for MPEG transport stream video segments (the format behind HLS streaming and broadcast captures). A text editor opens one and a video player opens the other, and a filename cannot tell you which you have. The content can: a TypeScript file is plain text that starts with code, while a transport stream is binary and begins with the sync byte 0x47 repeating every 188 bytes. This is a case where byte-level detection matters more than the extension.
Technical Specifications
- MIME Type:
text/typescript(also seen asapplication/typescriptandtext/x-typescript) - File Extensions:
.ts(source),.tsx(with JSX),.d.ts(type declarations),.mts/.cts(explicit ES module / CommonJS) - Category: Code
- Encoding: UTF-8 plain text
- First released: October 2012
- Designed by: Anders Hejlsberg at Microsoft
- Licence: Apache 2.0, open source
- Compiles to: JavaScript (any ECMAScript target from ES3 to ESNext)
Recognising TypeScript source
There is no magic number; it is text. The markers that separate it from plain JavaScript are type annotations and type-only constructs:
interface User {
id: number;
name: string;
email?: string;
}
function greet(user: User): string {
return `Hello, ${user.name}`;
}
const ids: number[] = [1, 2, 3];
type Status = "active" | "archived";
enum Role { Admin, Editor, Viewer }
interface, type aliases, enum, : number annotations, generics such as Array<T>, and the as and satisfies keywords all signal TypeScript. A .ts file with none of them is valid TypeScript too, since every JavaScript program is.
More Information
TypeScript was developed by Microsoft and first released in 2012. It was created to address the challenges of building large-scale JavaScript applications by providing better tooling through static typing, interfaces, and advanced IDE support. The language has gained significant adoption in the web development community and is used by major companies like Microsoft, Google, Slack, and Airbnb.
TypeScript provides developers with enhanced productivity through features like autocompletion, type checking, and refactoring tools. It helps catch errors at compile time rather than runtime, making applications more robust and maintainable.
TypeScript Format
TypeScript extends JavaScript with additional syntax for type annotations and advanced language features:
Key Features
- Static typing: Optional type annotations for variables, functions, and objects
- Interfaces: Define contracts for object structures
- Classes: Object-oriented programming with inheritance and access modifiers
- Generics: Reusable components that work with multiple types
- Decorators: Metadata annotations for classes and methods
- Modules: Import/export system for code organization
Type System
- Basic types: string, number, boolean, array, object
- Union types: Variables that can be one of several types
- Literal types: Exact values as types
- Utility types: Built-in type manipulation helpers
- Mapped types: Transform existing types into new ones
Compilation Options
- Target ES version: ES5, ES6, ES2020, etc.
- Module system: CommonJS, ES6, AMD, UMD
- Source maps: Debug original TypeScript code
- Declaration files: Generate .d.ts files for libraries
The TypeScript file family
| Extension | Contains | Notes |
|---|---|---|
.ts |
TypeScript source | The common case |
.tsx |
TypeScript with JSX | React components; Magika reports these as tsx |
.d.ts |
Type declarations only | No runtime code; describes the shape of a JavaScript library |
.mts |
TypeScript as an ES module | Compiles to .mjs |
.cts |
TypeScript as CommonJS | Compiles to .cjs |
tsconfig.json |
Compiler configuration | JSON, lives at the project root |
Declaration files are the ones most often found without context: a .d.ts file in a node_modules folder or a types/ directory is a type description of a package, not code that runs.
How to Open a TypeScript File
Just to read it
Any text editor works, since the file is plain UTF-8 text: Notepad or Notepad++ on Windows, TextEdit on macOS, gedit or nano on Linux. For syntax highlighting without installing anything, GitHub, GitLab and VS Code for the Web render .ts files directly in a browser.
To work with it
- Visual Studio Code: TypeScript support is built in, including type checking, go-to-definition and refactoring. This is the editor the language team develops against.
- WebStorm: full TypeScript integration from JetBrains.
- Visual Studio: TypeScript tooling ships with the Node.js and web workloads.
- Neovim / Emacs: the
typescript-language-servergives the same features through LSP.
To run it
TypeScript does not execute directly; it compiles to JavaScript first, or a runtime strips the types on the fly:
# Compile to JavaScript with the official compiler
npm install -D typescript
npx tsc hello.ts # writes hello.js next to it
# Run directly, no compile step
node --experimental-strip-types hello.ts # Node 22.6+
npx tsx hello.ts # tsx, a fast esbuild-based runner
deno run hello.ts # Deno runs TypeScript natively
bun run hello.ts # so does Bun
Development Tools
- TypeScript Compiler (tsc): Official command-line compiler
- tsx and ts-node: Execute TypeScript directly in Node.js
- tsc --watch: Automatic recompilation on file changes
- TypeScript Playground: Online TypeScript compiler and editor
Build Tools
- Vite: Fast build tool with native TypeScript support
- esbuild: Extremely fast TypeScript compiler and bundler
- Webpack: Bundle TypeScript with ts-loader or babel-loader
- Rollup: Module bundler with TypeScript plugin
- Parcel: Zero-configuration TypeScript bundling
How to Convert TypeScript to JavaScript
Conversion is compilation. The compiler removes the type annotations and, depending on the target, rewrites newer syntax for older runtimes:
# One file
npx tsc app.ts --target es2020 --module esnext
# A whole project, driven by tsconfig.json
npx tsc
# Types only, no JavaScript output (for checking in CI)
npx tsc --noEmit
A minimal tsconfig.json:
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"strict": true,
"outDir": "dist",
"declaration": true
},
"include": ["src"]
}
Going the other way, JavaScript to TypeScript, needs no conversion at all: rename .js to .ts, then add types incrementally. allowJs in tsconfig.json lets a project mix both during a migration.
Common Problems
- "Cannot find module" or missing types: install the declaration package, usually
npm install -D @types/<library>. - A
.tsfile that will not open in an editor and looks like garbage: it is almost certainly an MPEG transport stream video, not code. Open it in VLC. tscreports errors but the code runs: TypeScript still emits JavaScript on type errors unlessnoEmitOnErroris set. The errors are real; the output is just not blocked.- Browser cannot load a
.tsfile: browsers only run JavaScript. The file must be compiled or served through a dev server such as Vite that compiles on request.
Common Use Cases
TypeScript is widely used for:
- Web applications: Frontend frameworks like Angular, React, Vue
- Node.js applications: Backend services and APIs
- Desktop applications: Electron apps
- Mobile apps: React Native, Ionic
- Libraries and frameworks: Type-safe npm packages
- Large-scale applications: Enterprise software development
Related Formats
- JAVASCRIPT: what every TypeScript file compiles to.
- JSON: the format of
tsconfig.jsonandpackage.json. - MP4 and MKV: if your
.tsfile turned out to be video, these are the containers it is usually converted into. - PYTHON: another language where type annotations are optional and checked by separate tooling.
File Information
TypeScript source
Code
.ts
text/typescript
Related File Types
Other file types in the Code category you might also need:
Start Analyzing TYPESCRIPT Files Now
Use our free AI-powered tool to detect and analyze TypeScript source files instantly with Google's Magika technology.
⚡Try File Detection Tool