TypeScript is a language that adds types to JavaScript. You write code stating what kind of data each variable and function expects, and the compiler warns about errors before the program runs.
How it works
Every valid JavaScript code is also valid TypeScript. The difference is that you can annotate types. Browsers and Node.js do not run TypeScript directly: the code is converted to JavaScript before, in a step called compilation.
In this step, the compiler checks if the types make sense. If you try to add a number with text where that is not allowed, it shows the error in the editor.
npm install --save-dev typescript
npx tsc arquivo.tsProject configuration lives in the tsconfig.json file.
Practical example
Imagine a function that calculates the final price of an order. In plain JavaScript, someone can pass the value as text by mistake and only discover the problem in production.
In TypeScript, you declare the types:
function precoFinal(valor: number, desconto: number): number {
return valor - valor * desconto;
}
precoFinal("100", 0.1); // erro apontado pelo compiladorThe editor flags the wrong call right away, before any customer sees the problem.
What it's for
TypeScript is used to:
- Find errors early, still in the editor.
- Make maintenance of large projects and teams with several people easier.
- Improve autocompletion and code navigation.
- Document, through types, what each function expects and returns.
For those who use AI agents to program, types work as a safety net: if the agent generates incompatible code, the compiler flags the error, which helps with review. Many AI SDKs also offer TypeScript support.
Caveats
Types only apply at development time. Data coming from outside, such as API responses, still needs to be validated when the program runs. In addition, there is a small learning curve and an extra compilation step.
Difference between TypeScript and JavaScript
JavaScript does not require types and runs directly. TypeScript adds types and needs to be compiled to JavaScript. In exchange for the extra work, it delivers more safety and better editor support.