Skip to content
Sign inGet all access
Chapter 1·12 minFree

Getting Started

Checking a file with tsc, reading its errors, and types that vanish.

Checking a file

A .ts file is a .js file with types added. The only new thing here is : string:

app.ts
function greet(person: string) {
  return `hi ${person}`
}

console.log(greet("ada"))
Output$ npx tsc --noEmit app.ts$ echo $?0

Your first error

Pass the wrong kind of value and the silence breaks:

app.ts
function greet(person: string) {
  return `hi ${person}`
}

console.log(greet(42))
Output$ npx tsc --noEmit app.tsapp.ts(5,19): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.$ echo $?1

Annotating

A colon and a type, after the name. That is the whole syntax:

app.ts
function area(width: number, height: number) {
  return width * height
}

console.log(area(3, 4))
Output$ npx tsc --noEmit app.ts$ echo $?0

Those two annotations are the only reason the next version is caught:

app.ts
function area(width: number, height: number) {
  return width * height
}

console.log(area(3, "4"))
Output$ npx tsc --noEmit app.tsapp.ts(5,21): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.

Inference does most of it

Nothing here is annotated, and it still checks clean:

app.ts
const city = "London"
let count = 0

console.log(city, count)
Output$ npx tsc --noEmit app.ts$ echo $?0

The types are there all the same. Assign the wrong kind of value and you see them:

app.ts
let count = 0

count = "zero"
Output$ npx tsc --noEmit app.tsapp.ts(3,1): error TS2322: Type 'string' is not assignable to type 'number'.

Running the code

Once it checks clean, node runs the file directly — no build step, no loader:

app.ts
function greet(person: string) {
  return `hi ${person}`
}

console.log(greet("ada"))
Output$ npx tsc --noEmit app.ts$ node app.tshi ada

But node deletes the annotations without checking them, so it runs a broken file just as happily — same two commands, and the error changes nothing:

app.ts
function greet(person: string) {
  return `hi ${person}`
}

console.log(greet(42))
Output$ npx tsc --noEmit app.tsapp.ts(5,19): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.$ node app.tshi 42

Compiling

For a browser or a published package you need real JavaScript files. Drop --noEmit and tsc writes them:

src/main.ts
export function add(a: number, b: number) {
  return a + b
}
terminal
npx tsc --outDir dist src/main.ts
cat dist/main.js
Output$ npx tsc --outDir dist src/main.ts$ cat dist/main.jsexport function add(a, b) { return a + b;}

Types vanish

An interface describes a shape. It exists for the checker and nowhere else:

app.ts
interface User {
  name: string
}

const u: User = { name: "ada" }

console.log(u.name)
Output$ npx tsc --noEmit app.ts$ echo $?0

Here is what actually runs. The interface is simply gone:

dist/app.js
const u = { name: "ada" }

console.log(u.name)

Reading an error

Three more worth recognising. A missing property:

app.ts
interface User {
  name: string
  age: number
}

const u: User = { name: "ada" }
Output$ npx tsc --noEmit app.tsapp.ts(6,7): error TS2741: Property 'age' is missing in type '{ name: string; }' but required in type 'User'.

A method that does not exist on the type:

app.ts
const nums = [1, 2, 3]

console.log(nums.toUpperCase())
Output$ npx tsc --noEmit app.tsapp.ts(3,18): error TS2339: Property 'toUpperCase' does not exist on type 'number[]'.

And a property you never declared:

app.ts
interface User {
  name: string
}

const u: User = { name: "ada", age: 36 }
Output$ npx tsc --noEmit app.tsapp.ts(5,32): error TS2353: Object literal may only specify known properties, and 'age' does not exist in type 'User'.