A .ts file is a TypeScript source code file — the plain-text document where you write TypeScript code before it gets converted into JavaScript that browsers and servers can run.
TypeScript is a programming language built on top of JavaScript that adds type checking — a way to declare what kind of data a variable should hold (a number, a string, a list, and so on) so mistakes get caught before the code runs. When you write TypeScript, you save it with a .ts extension. Later, a tool called a compiler reads that .ts file, checks the types you declared, and outputs a .js file with regular JavaScript that actually executes.
The .ts extension tells your code editor and build tools that the file contains TypeScript, not plain JavaScript. This matters because TypeScript has syntax JavaScript doesn't understand — like type annotations — so the file needs to be processed before it can run anywhere.
Key Takeaways
- A .ts file contains TypeScript code, which is JavaScript with added type checking that must be compiled into .js files before it can run.
- TypeScript catches type errors during development rather than when the code runs, which prevents many common bugs.
- You write .ts files in a code editor, then a compiler (usually built into your project's build tool) converts them to .js automatically.
- The .ts extension signals to your editor and tools that the file uses TypeScript syntax, not plain JavaScript.
- Large projects and teams often use TypeScript because the type checking makes code easier to refactor and understand months later.
How TypeScript Files Get Converted to JavaScript
When you save a .ts file, it does not run directly. Instead, a TypeScript compiler reads it, checks all the type declarations you wrote, and outputs a .js file with the same logic but in plain JavaScript. This process is called compilation or transpilation.
Most projects automate this. If you are using a build tool like Webpack, Vite, or Create React App, the compiler runs automatically every time you save a .ts file. You see the output .js file appear in your project folder (usually in a dist or build directory), and that .js file is what actually gets sent to browsers or servers. You almost never write the .js file by hand — the compiler generates it.
The compiler also reports errors. If you declare that a variable is a number but then try to assign it text, the compiler catches that and stops the build, forcing you to fix the mistake before the code can run. This happens at development time, not when a user runs your code.
Type Annotations: What Makes TypeScript Different
The main reason to use a .ts file instead of a .js file is type annotations — explicit declarations of what kind of data a variable, function parameter, or return value should be. In plain JavaScript, you can assign any type of data to any variable, which makes mistakes easy to miss.
In TypeScript, you can write something like let userName: string = "Alice" to declare that userName must always be text. If another part of your code later tries to assign a number to userName, the compiler catches it immediately and tells you the line number. In a large project, this prevents hours of debugging.
You can also define custom types for complex data structures. For example, you might define a User type that says "a User must have a name (string), an age (number), and an email (string)". Then every function that works with users knows exactly what properties to expect, and the compiler enforces it.
When Projects Use .ts Files
Small scripts and simple projects often stick with plain .js files because the overhead of setting up a TypeScript compiler is not worth it. But larger projects — especially those with multiple developers or code that will be maintained for years — usually switch to .ts files because the type checking catches bugs early and makes the code easier to understand and change.
Web frameworks like React, Vue, and Angular all support .ts files natively. If you create a new React project with Create React App or Vite, you get the option to use TypeScript from the start, and the build tool handles the compilation for you. Backend projects using Node.js also commonly use .ts files, compiled to .js before the server runs.
Teams also use .ts files because they serve as documentation. When a function declares its parameter types and return type, anyone reading the code knows exactly what data it expects and what it produces, without having to trace through the entire function.
The .ts File Extension vs. Other TypeScript Files
TypeScript has a few related file extensions. A .ts file holds regular TypeScript code. A .tsx file is a TypeScript file that also contains JSX — the syntax for writing HTML-like code inside JavaScript, used in React components. The compiler handles both the same way, but the .tsx extension tells your editor that the file mixes TypeScript and JSX.
You might also see .d.ts files, which are type definition files. These files declare types without containing any actual code — they are used to describe the types in a JavaScript library so TypeScript projects can use that library with type checking. You rarely write .d.ts files by hand; they are usually generated automatically or provided by the library author.
Setting Up a Project to Use .ts Files
To start using .ts files, you need a TypeScript compiler installed in your project. Most modern project templates (Create React App, Vite, Next.js) include it automatically. If you are setting up from scratch, you install the TypeScript package using npm or yarn, then create a tsconfig.json file that tells the compiler how strict to be about type checking and where to output the compiled .js files.
Once the compiler is set up, you simply create .ts files and write TypeScript code. Your build tool watches for changes and compiles automatically. During development, your editor (like Visual Studio Code) reads the tsconfig.json and shows you type errors in real time, before you even save the file.
The learning curve is real — TypeScript syntax takes time to learn, and beginners often find type annotations tedious at first. But most developers who use it regularly say the time spent writing types pays off in fewer bugs and easier refactoring later.
Common Mistakes When Working With .ts Files
A frequent mistake is trying to run a .ts file directly with Node.js. Node.js only understands .js files, so if you try to run a .ts file, it will fail. You must compile it first, or use a tool like ts-node that compiles on the fly. Most projects automate this, so you do not have to think about it, but it is worth knowing why it happens.
Another common issue is forgetting to update the tsconfig.json when you move files or change your project structure. The compiler uses that file to know where to look for .ts files and where to put the compiled .js output. If the paths are wrong, the compiler may not find your files or may output them to the wrong place.
New TypeScript users also sometimes declare types too strictly, making the code harder to write without gaining much benefit. Learning when to use strict types and when to let TypeScript infer types (figure them out automatically) takes practice.
Frequently Asked Questions
Can I use .ts files in a browser directly?
No. Browsers only understand JavaScript (.js files), not TypeScript. You must compile .ts files to .js first. Build tools like Webpack or Vite do this automatically during development and before you deploy to production.
Do I have to use TypeScript if my project uses .ts files?
Yes. A .ts file is specifically a TypeScript file. If you want to use plain JavaScript, use .js files instead. You can mix .ts and .js files in the same project, but each .ts file must be valid TypeScript.
What happens if the TypeScript compiler finds an error?
The compiler stops and reports the error with a line number and description. Your build fails, and no .js file is generated. You must fix the type error in the .ts file before the compiler will proceed. This is intentional — it prevents broken code from running.
Is TypeScript slower than JavaScript?
TypeScript itself adds no runtime overhead. The compiled .js file runs at the same speed as hand-written JavaScript. The only cost is compilation time during development, which is usually fast enough that you do not notice it.
Can I rename a .ts file to .js and keep using it?
Only if the file contains valid JavaScript with no TypeScript-specific syntax (like type annotations). If it uses TypeScript features, renaming it to .js will cause errors because JavaScript does not understand those features. You would need to remove all the type annotations first.