Setting Up and the Basic Types

Harry · 14 Sep 2026 · 2 views
Advertisement
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
  • any disables type checking – use it as a last resort.
  • unknown is the safe alternative: you must check the type before using it.
  • void is the return type of a function that returns nothing.

Key points

  • Install with npm; compile with tsc; configure via tsconfig.json.
  • Primitives are string, number, boolean; annotate with : Type.
  • Inference covers initialised variables – mainly annotate function signatures.
  • Prefer unknown over any to keep type safety.
Share this post:

Comments (0)

Please login or register to comment.