TypeScript · Lesson 2 of 7

Basic types & inference

string, number, boolean, arrays, tuples, any vs unknown, and letting TypeScript infer types.

The basic types are string, number, boolean, null and undefined. Arrays are written string[] or Array<string>; a tuple is a fixed-length array such as [string, number].

You don't have to annotate everything. TypeScript infers types from values: let city = "Kochi" is already a string. Annotate function parameters and public APIs; let inference handle local variables.

any vs unknown

  • any switches type checking off — avoid it.
  • unknown means “could be anything”, but you must check the type before using it. Use it for data you don't control yet.

Example code

Try it in the TypeScript Playground or with tsc
let city = "Kochi";             // inferred as string
city = 42;                       // error

const skills: string[] = ["TS", "React"];
const salary: [string, number] = ["LPA", 6.5];

function show(value: unknown) {
  if (typeof value === "string") {
    console.log(value.toUpperCase()); // OK: narrowed to string
  }
}

show("typescript");

Compiler / program output

error TS2322: Type 'number' is not assignable to type 'string'.

(after fixing that line, the program prints)
TYPESCRIPT

Paste the code into the official TypeScript Playground in your browser, or install TypeScript (npm install -D typescript) and run npx tsc --noEmit to type-check. Names are fictional.

Key points

  • Basic types: string, number, boolean, null, undefined.
  • Arrays: string[]; tuples: [string, number].
  • Let TypeScript infer local variable types.
  • Prefer unknown over any, and check before use.

Quick check

Which type forces you to check the value before using it?

Practice task

Declare a tuple for a job (title, salary in LPA, isRemote) and an array of three such tuples, then loop and print each one.

Interview corner

What is type inference?

TypeScript works out a variable's type from the value assigned or a function's return type from its return statements, so you don't need to write every annotation.

What is the difference between any and unknown?

Both accept any value. any disables type checking completely; unknown requires narrowing before you can use the value, so it keeps type safety.

Further reading

Official documentation, linked for deeper study. These are outside sites; this lesson's text is our own.

Original lesson written by the InfoparkDaily team · © 2026 InfoparkDaily · Free to learn — no paid course or subscription.