Skip to content
BENIBIX Learn

TypeScript

Development🔤 LanguagesPopular Languages

Also known as TS

JavaScript with types added. It catches lots of mistakes before the code runs.

Think of it like

Think of it like labels on kitchen jars. The labels stop you putting salt in the sugar jar.

Example

Microsoft made TypeScript, and its VS Code editor is written in it. Jack types "twelve" where a number goes, and the line turns red.

Why it matters

Big JavaScript projects get hard to change safely. Types let the editor point out what a change will break.

Definition

TypeScript is JavaScript with types added. You can say a value must be a number, a string or a certain shape of object. A checker reads the code and warns you before it runs if a type is wrong. A tool then turns it into plain JavaScript, so it runs anywhere JavaScript does. It came out in 2012, and many big web apps now use it.

Example code

typescript
let age: number = "twelve";
// error TS2322: Type 'string' is not assignable to type 'number'.
The checker stops the wrong kind of value before the code runs.

Common mistake

Thinking TypeScript checks values while the app runs. The checks happen before; at run time it is plain JavaScript.

Key takeaways

  1. 1
    JavaScript plus types. Every JavaScript file is almost valid TypeScript.
  2. 2
    Checked early. Mistakes show up as you type.
  3. 3
    Runs as JavaScript. A tool strips the types away.

Compare

Learn it properly