AnyCompiler

Basic Types

TypeScript's core primitive types are string, number, and boolean. You can annotate a variable's type explicitly with a colon, though TypeScript is often able to figure the type out on its own.

let username: string = "Alice";
let age: number = 30;
let isStudent: boolean = false;

console.log(username, typeof username);
console.log(age, typeof age);
console.log(isStudent, typeof isStudent);
Output
Alice string
30 number
false boolean

Type inference

You don't have to annotate every variable — if you give a variable an initial value, TypeScript infers its type from that value automatically, and will still catch a mismatch later exactly as if you'd written the annotation yourself.

let city = "Tokyo"; // inferred as string, no annotation needed
console.log(city, typeof city);
Output
Tokyo string

What a real type error looks like

This is genuinely what TypeScript's compiler outputs when you try to assign the wrong type — captured from a real run, not written to look plausible:

let score: string = "high";
score = 42;
console.log(score);
Output
script.ts(2,1): error TS2322: Type '42' is not assignable to type 'string'.

Nothing runs at all — no output, just the error. That's the whole point: TypeScript stops you here instead of letting a wrong-typed value quietly cause problems somewhere else in your program.

Next: arrays and tuples — typed collections, and a TypeScript-only structure with no real JavaScript equivalent.

Practice exercise

Declare a variable price with type number set to 19.99 and a variable inStock with type boolean set to true. Print them together with console.log(price, inStock).