Setting Up and the Basic Types
Harry
· 14 Sep 2026
· 2 views
Advertisement
Install and compile
npm install -g typescript
tsc --version
# compile a file to JavaScript
tsc app.ts # produces app.js
For a project, generate a config file with tsc --init; it creates tsconfig.json where you set the target JS version, strictness and output folder. Then just run tsc to build the whole project.
Annotating types
Add a type after a colon. The basic (primitive) types are string, number, boolean:
let title: string = "Docs";
let count: number = 42;
let active: boolean = true;
Type inference
You rarely need to write types for initialised variables – TypeScript infers them:
let name = "Ada"; // inferred as string
name = 5; // Error: number not assignable to string
Annotate function parameters and return types (which cannot be inferred from a call site), and let inference handle the rest.
Arrays, tuples and special types
let nums: number[] = [1, 2, 3];
let pair: [string, number] = ["age", 36]; // tuple: fixed shape
let anything: any; // opts out of checking — avoid
let value: unknown; // safe 'any' — must narrow before use
function log(msg: string): void { } // returns nothing
anydisables type checking – use it as a last resort.unknownis the safe alternative: you must check the type before using it.voidis the return type of a function that returns nothing.
Key points
- Install with npm; compile with
tsc; configure viatsconfig.json. - Primitives are
string,number,boolean; annotate with: Type. - Inference covers initialised variables – mainly annotate function signatures.
- Prefer
unknownoveranyto keep type safety.