TypeScript é JavaScript com sintaxe para tipos.

TypeScript é uma linguagem de programação fortemente tipada que se baseia em JavaScript, oferecendo melhores ferramentas em qualquer escala.

ts
const user = {
firstName: "Angela",
lastName: "Davis",
role: "Professor",
}
 
console.log(user.name)
Property 'name' does not exist on type '{ firstName: string; lastName: string; role: string; }'.2339Property 'name' does not exist on type '{ firstName: string; lastName: string; role: string; }'.
 
ts
const user = {
firstName: "Angela",
lastName: "Davis",
role: "Professor",
}
 
console.log(user.name)
Property 'name' does not exist on type '{ firstName: string; lastName: string; role: string; }'.2339Property 'name' does not exist on type '{ firstName: string; lastName: string; role: string; }'.
 

TypeScript 5.4 está disponível agora, 5.5 está atualmente em beta.

O que é TypeScript?

JavaScript e Mais

O TypeScript adiciona sintaxe adicional ao JavaScript para oferecer suporte a uma integração mais estreita com seu editor. Detecte erros mais cedo em seu editor.

Um Resultado Que Você Pode Confiar

O código TypeScript é convertido em JavaScript, que funciona em qualquer lugar em que o JavaScript é executado: em um navegador, em Node.js ou Deno e em seus aplicativos.

Segurança em Escala

O TypeScript entende JavaScript e usa inferência de tipos para fornecer ótimas ferramentas sem código adicional.

Adote o TypeScript Gradualmente

Aplique tipos ao seu projeto JavaScript de forma incremental, cada etapa melhora o suporte ao editor e melhora sua base de código

Vamos pegar esse código JavaScript incorreto e ver como o TypeScript pode detectar erros em seu editor.

js
function compact(arr) {
if (orr.length > 10)
return arr.trim(0, 10)
return arr
}

No editor warnings in JavaScript files

This code crashes at runtime!

Arquivo JavaScript

js
// @ts-check
 
function compact(arr) {
if (orr.length > 10)
Cannot find name 'orr'.2304Cannot find name 'orr'.
return arr.trim(0, 10)
return arr
}

Adding this to a JS file shows errors in your editor

the param is arr, not orr!

JavaScript com TS Check

js
// @ts-check
 
/** @param {any[]} arr */
function compact(arr) {
if (arr.length > 10)
return arr.trim(0, 10)
Property 'trim' does not exist on type 'any[]'.2339Property 'trim' does not exist on type 'any[]'.
return arr
}

Using JSDoc to give type information

Now TS has found a bad call. Arrays have slice, not trim.

JavaScript com JSDoc

ts
function compact(arr: string[]) {
if (arr.length > 10)
return arr.slice(0, 10)
return arr
}

TypeScript adds natural syntax for providing types

Arquivo TypeScript

Describe Your Data

Describe the shape of objects and functions in your code.

Making it possible to see documentation and issues in your editor.

ts
interface Account {
id: number
displayName: string
version: 1
}
 
function welcome(user: Account) {
console.log(user.id)
}
ts
type Result = "pass" | "fail"
 
function verify(result: Result) {
if (result === "pass") {
console.log("Passed")
} else {
console.log("Failed")
}
}

TypeScript becomes JavaScript via the delete key.

ts
type Result = "pass" | "fail"
 
function verify(result: Result) {
if (result === "pass") {
console.log("Passed")
} else {
console.log("Failed")
}
}
 

TypeScript file.

ts
type Result = "pass" | "fail"
 
function verify(result: Result) {
if (result === "pass") {
console.log("Passed")
} else {
console.log("Failed")
}
}
 

Types are removed.

js
 
 
function verify(result) {
if (result === "pass") {
console.log("Passed")
} else {
console.log("Failed")
}
}
 

JavaScript file.

Historias de Migrações

First, we were surprised by the number of small bugs we found when converting our code.

Second, we underestimated how powerful the editor integration is.

TypeScript was such a boon to our stability and sanity that we started using it for all new code within days of starting the conversion.

Felix Rieseberg at Slack covered the transition of their desktop app from JavaScript to TypeScript in their blog

Read

Amado por desenvolvedores

Image of the stack overflow logo, and a graph showing TypeScript as the 2nd most popular language

Voted 2nd most loved programming language in the Stack Overflow 2020 Developer survey

Logo of the State of JS survey

TypeScript was used by 78% of the 2020 State of JS respondents, with 93% saying they would use it again.

TypeScript was given the award for “Most Adopted Technology” based on year-on-year growth.