Basic Types

TypeScript covers all of JavaScript's basic types — string, number, boolean, arrays, and a few TypeScript-specific ones — with a syntax for writing them explicitly, and a compiler that's often smart enough to figure them out without being told.

The core primitives

TS basics.ts
let username: string = "jordan";
let age: number = 29;
let isAdmin: boolean = false;

console.log(username, age, isAdmin);
Output
jordan 29 false

Unlike C or Java, TypeScript only has one numeric type, number, covering both integers and decimals — there's no separate int or float.

Type inference

Writing out : string everywhere gets repetitive, and usually isn't necessary — TypeScript infers a variable's type from whatever it's initialized with:

TS inference.ts
let city = "Lisbon";
city = 42;
tsc output
inference.ts:2:1 - error TS2322: Type 'number' is not
assignable to type 'string'.

Even without an annotation, city is locked in as a string the moment it's initialized with one — TypeScript inferred the type from the value and enforces it from then on.

Arrays and any

TS arrays.ts
let scores: number[] = [88, 92, 79];
let names: Array<string> = ["Ana", "Ben"];

scores.push("nope");
tsc output
arrays.ts:4:13 - error TS2345: Argument of type 'string' is
not assignable to parameter of type 'number'.

number[] and Array<number> are two ways of writing the exact same type — an array whose every element must be a number. Either syntax works; most codebases just pick one style and stay consistent.

Note: TypeScript also has an escape hatch called any — a type that turns off checking entirely for that value. let data: any = fetchSomething(); compiles no matter what you do with data afterward, which defeats the entire point of using TypeScript in the first place. It's sometimes unavoidable at the boundary with untyped JavaScript, but reaching for it inside your own code is usually a sign you haven't modeled the data properly yet — unknown is almost always the safer choice when you genuinely don't know a type ahead of time, since it forces you to check before using it.