let name: string = 'Sam';
let age: number = 30;
let isActive: boolean = true;
Topics
26
Classes: Access Modifiers, Abstract Classes & Implements
Conditional Types
Declaration Files
Decorators
Discriminated Unions & Exhaustiveness Checking
Enums
Function Types, Overloads & Optional/Default Parameters
Generics
Index Signatures & Record Types
Mapped Types
Migrating JavaScript to TypeScript
Modules
Namespaces & Declaration Merging
Never, Unknown & Void Types
Nullability: strictNullChecks, Optional Chaining & Nullish Coalescing
Primitive Types, Arrays & Tuples
Readonly, const Assertions & Immutability
Structural Typing & Duck Typing
Template Literal Types
tsconfig & Compiler Options
Type Assertions & Type Casting
Type Inference & Contextual Typing
Type Narrowing & Guards
Types & Interfaces
Union & Intersection Types
Utility Types
Primitive Types, Arrays & Tuples
11 questions found
TypeScript's core primitives are string, number, boolean, null, undefined, symbol, and bigint — annotate a variable using a colon followed by the type name after its identifier.
Real-world example
Explicitly annotating a function parameter's expected primitive type to catch incorrect calls at compile time.
Never
Unknown & Void Types
How do you type an array of a specific element type, and what are the two equivalent syntaxes?
BeginnerUse either `Type[]` (more common, especially for simple types) or the generic `Array<Type>` syntax (sometimes preferred for more complex or nested generic types, for readability) — both are functionally identical.
let scores: number[] = [90, 85, 78];
let names: Array<string> = ['Sam', 'Alex'];
// Array<Array<number>> can be more readable than number[][]
Real-world example
Typing a function parameter that accepts a list of user objects or numeric scores.
Generics
A tuple is a fixed-LENGTH array where each position has its own SPECIFIC type, unlike a regular array type where every element must share the same single type — tuples are declared with square brackets listing each position's type in order.
let point: [number, number] = [3, 4]; // exactly two numbers
let entry: [string, number] = ['age', 30]; // first is string, second is number
// let bad: [number, number] = [1, 2, 3]; // Error: too many elements
Real-world example
Typing a coordinate pair, a key-value entry, or a fixed-shape function return like [value, error].
Destructuring
Spread & Rest
How do you make a tuple element optional, and how does that affect the tuple's minimum required length?
IntermediateAdd '?' after a tuple element's type, similar to optional function parameters — optional elements must come after all required elements, and TypeScript correctly tracks that the tuple's minimum length no longer includes the optional trailing elements.
type Point = [x: number, y: number, z?: number];
const p2d: Point = [3, 4]; // OK, z omitted
const p3d: Point = [3, 4, 5]; // OK, z included
Real-world example
Typing a 2D-or-3D coordinate tuple where the z-axis is only sometimes relevant.
Function Types
Overloads & Optional/Default Parameters
How do labeled tuple elements improve readability and editor tooling compared to unlabeled tuples?
IntermediateAdding a name before each element's type (like `[x: number, y: number]`) doesn't change runtime behavior at all, but significantly improves editor autocomplete/hover tooltips and function signature readability — especially valuable for tuples used as function parameter lists or return values with multiple positions.
function move([deltaX, deltaY]: [x: number, y: number]) {
console.log(`Moving by ${deltaX}, ${deltaY}`);
}
// hovering the tuple type in an editor now shows 'x' and 'y' labels instead of just 'number, number'
Real-world example
Improving the developer experience of a utility function's tuple-typed return value, like a custom useToggle() hook returning [value, toggle].
Generics
A tuple can end with a rest element (using '...Type[]') to require a FIXED set of leading elements of specific types, followed by any number (including zero) of additional elements all sharing the rest element's type — combining tuple precision with array flexibility.
type StringThenNumbers = [string, ...number[]];
const a: StringThenNumbers = ['label']; // OK, zero numbers
const b: StringThenNumbers = ['label', 1, 2, 3]; // OK, three numbers
Real-world example
Typing a function's arguments where the first argument has a fixed meaning (like a format string) followed by a variable number of typed values.
Function Types
Overloads & Optional/Default Parameters
How does TypeScript infer a tuple type versus a regular array type from an array literal, and how do 'as const' assertions change this?
AdvancedBy default, an array literal like `[1, 2]` is inferred as the wider `number[]` array type, NOT a tuple — to get a precise, fixed-length tuple type inferred automatically, apply `as const`, which locks both the literal element values AND the tuple's fixed length/readonly-ness.
const loose = [1, 2]; // inferred as number[]
const tuple = [1, 2] as const; // inferred as readonly [1, 2] -- a precise tuple of literal values
Real-world example
Getting precise literal-value tuple types for things like fixed configuration arrays or coordinate constants, without a manual type annotation.
Readonly
const Assertions & Immutability
How would you write a generic type that extracts the length of a tuple type at compile time?
AdvancedSince tuple types carry a real '.length' property whose VALUE is a specific literal number (not just 'number' like regular arrays), you can access it directly via an indexed access type on the tuple's 'length' key to extract that literal numeric type.
type TupleLength<T extends readonly unknown[]> = T['length'];
type Len = TupleLength<[string, number, boolean]>; // 3 (the literal number type, not just 'number')
Real-world example
Building advanced type-level utilities (like a type-safe zip or curry function) that need to reason about a tuple's exact arity at compile time.
Generics
How do variadic tuple types (using generic rest elements) let you build type-safe function composition or concat utilities?
AdvancedVariadic tuple types let a generic rest parameter (`...T extends unknown[]`) be spread and recombined within OTHER tuple positions, preserving each individual element's specific type through operations like concatenation — enabling fully type-safe utilities that work across tuples of arbitrary, differing lengths and element types.
type Concat<T extends unknown[], U extends unknown[]> = [...T, ...U];
type Result = Concat<[string, number], [boolean]>;
// [string, number, boolean] -- each position's specific type preserved
Real-world example
Building a fully type-safe function-argument concatenation utility, like a generic curry() or partial-application helper.
Generics
How does readonly interact with tuple types, and what does `readonly [number, number]` prevent compared to a mutable tuple?
AdvancedA readonly tuple prevents any mutation of its elements after creation — no reassigning an index, and critically, none of the mutating array methods (push, pop, splice, sort) are even available on its type, since they'd violate the fixed-length, immutable contract; you can still read elements and destructure normally.
function getOrigin(): readonly [number, number] {
return [0, 0];
}
const origin = getOrigin();
// origin[0] = 5; // Error: cannot assign to readonly tuple element
// origin.push(1); // Error: 'push' does not exist on type 'readonly [number, number]'
Real-world example
Returning an immutable coordinate pair or fixed-shape result from a function where callers shouldn't be able to mutate it.
Readonly
const Assertions & Immutability
Showing 1–10 of 11