Basic Types

The built-in labels TypeScript uses for strings, numbers, booleans, arrays and fixed-shape lists.

What is it?

TypeScript has a few basic type labels that match the values JavaScript already has:

  • string for text.
  • number for any number. There's no separate "integer" or "float" type.
  • boolean for true/false.
  • An array (a list) is written string[] or Array<string>. Both mean the same thing.

Sometimes you know exactly how many items a list has and what type sits at each position, like a pair that's always "a name, then an age." That fixed-length, fixed-order array is a tuple, written [string, number].

Finally, any tells TypeScript "stop checking this, it could be anything." It's tempting when you're unsure of a type, but it switches off the safety net for that value and lets mistakes slip through. Treat it as an escape hatch for rare cases, not a default.

Explain like I'm 10

Basic types are like labels on storage bins: "text only," "numbers only," "exactly one name and one age, in that order." The any bin has no label, so anything can go in. Convenient, but nothing stops you from adding something that doesn't belong.

Examples

The core primitive types

let username: string = "amara";
let age: number = 29;
let isActive: boolean = true;

// TypeScript catches mismatches immediately:
age = "twenty-nine";
// Error: Type 'string' is not assignable to type 'number'.

Each variable is annotated with the type it may hold. TypeScript enforces that on every later assignment.

Arrays, tuples, and any

let scores: number[] = [90, 85, 76];
let names: Array<string> = ["Sam", "Lee"];

// A tuple: always exactly [name, age], in that order
let profile: [string, number] = ["Priya", 34];

// any turns off checking for this value entirely — use sparingly
let response: any = fetchLegacyApi();
response.whatever.you.want(); // no error, even if this is wrong

Arrays hold any number of same-typed items. A tuple fixes the number of items and each one's type by position. any turns off type checking for a value: powerful but risky.

How it works

When you write let age: number = 29, the compiler records that age is a number. It checks every later use against that: reassignments, function calls, comparisons. It does this by reading the code (hence "static"). It never runs age = "twenty-nine" to find the mismatch.

any tells the compiler to skip that record for a value, so no checks run against it and mismatches slip through silently.

Why does it exist?

JavaScript already has strings, numbers, booleans and arrays at runtime. It just can't declare in advance which one a variable should hold. Basic types give you that vocabulary, so the compiler has something to check against.

When to use it

Add explicit types when a value's type isn't obvious: function parameters, empty arrays you'll fill later, and tuples where position matters (like an [x, y] coordinate). For something like let count = 0, TypeScript infers the type, so an annotation is just noise.

When not to use it

Don't use any as a default when you're unsure of a type. It silently turns off the checking you wanted. Work out the real type, or use unknown when you truly don't know yet. unknown is safer because it forces you to check the value before using it.

Common mistakes

  • Using any whenever a type is inconvenient, which turns off checking for that value and everything derived from it.

  • Confusing a tuple with an array. [string, number] is exactly two items in that order; (string | number)[] allows any number of items in any order.

  • Over-annotating obvious values, like let count: number = 0, when TypeScript infers number from the 0.

Practice exercises

  1. Easy:

    Declare three typed variables: a string for a city name, a number for its population and a boolean for whether it's a capital.

  2. Medium:

    Create a tuple type for an RGB color, [number, number, number], and a variable of that type. Then try adding a fourth number and read the error.

  3. Hard:

    Write a function describe(value: any) that logs value.length. Call it with a number and notice TypeScript doesn't catch the runtime error. Then change the parameter to unknown and see what TypeScript requires before you can access .length.

Interview questions

What's the difference between `string[]` and `[string, number]`?

string[] is an array of any length where every element is a string. [string, number] is a tuple: exactly two elements, a string then a number, checked by position.

Why is overusing `any` considered bad practice?

It turns off type checking for that value, so mistakes pass silently. That defeats the point of TypeScript for that code.

Does TypeScript have separate types for integers and floating-point numbers?

No. One number type covers all numeric values, matching how JavaScript represents numbers.

What's the key difference between `any` and `unknown`?

Both can hold any value. any turns off checking completely. unknown makes you narrow the value first (with a type guard, typeof check or assertion) before you can use it. It's the safe version of "could be anything."

Given `let x: unknown = getValue(); x.trim();`, does this compile?

No. x might hold a string at runtime, but its declared type is unknown, so TypeScript won't allow .trim() until you narrow it, for example with if (typeof x === "string") { x.trim(); }.

What type does TypeScript infer for `let count = 0;` when no annotation is written?

number. For a let (or var), TypeScript infers the broader type of the initializer, since the variable may be reassigned later.

What type does TypeScript infer for `const count = 0;`, and how is that different from the `let` case?

The literal type 0, not number. A const can't be reassigned, so its value is always exactly 0 and TypeScript keeps the more precise type.

Why does `let age: number = 29; age = "twenty-nine";` get rejected by TypeScript, even though the equivalent plain JavaScript runs fine?

JavaScript lets a variable hold any type at any time. TypeScript locks it: once age is annotated (or inferred) as number, every later assignment is checked, and a string fails.

Can you append a fourth item to a variable typed as `let profile: [string, number]` using `.push()`?

Surprisingly, often yes. push on a tuple is loosely checked and won't always error, even though reading a fourth element or assigning a longer tuple would be rejected. It's a well-known sharp edge with tuples and mutating array methods.

At runtime, is there any difference between an array declared as `string[]` and one declared as `Array<string>`?

No. They're two ways to write the same type. Both compile to an ordinary JavaScript array and are checked the same way.

Why does `let profile: [string, number] = [34, "Priya"];` fail to compile?

A tuple checks each position: the first slot must be string, the second number. The values are in the wrong order, so the assignment doesn't match even though both types are present.

What's the practical downside of writing `let scores = [];` instead of `let scores: number[] = [];`?

With nothing to infer from, TypeScript often infers any[], so nothing pushed into scores is checked. Declaring the element type keeps the contents checked from the start.

If you get back a JSON payload from an API and you don't yet know its exact shape, should you type it as `any` or `unknown`?

unknown. It holds any shape just as well, but forces you to check the value before using it. any would let wrong assumptions about the payload go unchecked.

Can a `boolean`-typed variable hold values like `0`, `""`, or `null` the way a JavaScript `if` condition treats them as falsy?

No. boolean accepts only true and false. Falsy values behave like false in a runtime condition, but they aren't boolean.

When would a tuple be a better fit than an interface for representing two related values, like a name and an age?

A tuple fits when position alone carries the meaning, like a function that always returns [value, error]. Use an interface when named fields help or there are more than a couple of values, since names are clearer than indexes.

Why is `any` sometimes called an "escape hatch," and why is it risky when it flows into other code?

It's for rare cases where you can't express a type. The risk is that any spreads: anything derived from it, like a property access or return value, is also any, so the loss of checking flows through the code that touches it.

What is *type widening*, and where does it show up with basic types?

TypeScript inferring a broader type for a mutable variable than its value. For example, let x = 5 is inferred as number, not the literal type 5, because x could later hold another number.

Why doesn't declaring `let count: number = 0;` gain you anything over just writing `let count = 0;`?

TypeScript already infers number from 0. The annotation adds noise but no safety.

Is `unknown` a subtype of every other type, the way `any` is often loosely described?

Not quite. any works in both directions: it can be assigned to anything and anything can be assigned to it. unknown accepts any value, but can't be assigned to a more specific type without narrowing first. That asymmetry is what makes it safer.

If a function parameter is typed `any`, does TypeScript check calls to methods on that parameter, like `param.whatever.you.want()`?

No. On an any value, TypeScript checks no property accesses, method calls or arguments, so that call compiles even if it would throw at runtime.