Skip to main content

What does the type keyword do?

What the type keyword does in TypeScript

The type keyword is used to create type aliases. That is, you can give a name to any type (a primitive, an object, a union, a function, an array, and so on) to reuse it and make the code clearer.


In simple terms

type is a way to define your own type based on existing ones.

javascript
type UserID = number; type UserName = string;

Now you can use them in other types:

javascript
type User = { id: UserID; name: UserName; };

Example: alias for a primitive type

javascript
type Age = number; let myAge: Age = 25;

This does not create a new data type in JS - it is only for type checking at compile time.


Example: object type

javascript
type User = { id: number; name: string; isAdmin?: boolean; // optional property }; const user: User = { id: 1, name: "Tim", };

You can define properties, optional fields (?), and nested types.


Example: union of types (union)

javascript
type Status = "loading" | "success" | "error"; let currentStatus: Status = "loading"; currentStatus = "error"; // allowed currentStatus = "done"; // Error

Very convenient when you need to restrict the allowed values (similar to an enum, but lighter).


Example: intersection of types (intersection)

You can combine several types into one:

javascript
type Person = { name: string }; type Contact = { email: string }; type Employee = Person & Contact; const worker: Employee = { name: "Alex", email: "alex@example.com", };

Example: typing functions

javascript
type Logger = (message: string, level?: "info" | "error") => void; const log: Logger = (msg, level = "info") => { console.log(`[${level}] ${msg}`); };

Example: typing arrays and objects

javascript
type IDList = number[]; const ids: IDList = [1, 2, 3];

or

javascript
type Dictionary = Record<string, number>; const scores: Dictionary = { math: 95, physics: 90, };

type vs interface

Featuretypeinterface
Can describe objectsyesyes
Can combine via &yesno (only extends)
Can describe unions (A | B)yesno
Can describe primitives, functions, tuples, etc.yesno
Can be extendedonly via &yes
Can be redeclared and mergednoyes

Example:

javascript
type A = { a: number }; type B = { b: string }; type AB = A & B; // { a: number; b: string } interface IA { a: number; } interface IB extends IA { b: string; }

Recommendation:

  • Use type for unions, functions, tuples, union types.
  • Use interface for data structures (for example, objects, API models).

Example: type + as const

javascript
const ROLES = ["admin", "user", "guest"] as const; type Role = typeof ROLES[number]; // "admin" | "user" | "guest" let myRole: Role = "admin"; // allowed

Summary

PropertyDescription
PurposeCreating custom type aliases
Works at the level ofCompilation (does not exist in JS)
SupportsPrimitives, functions, objects, unions, intersections
Used forCode reuse and readability
JS equivalentNone (this is TypeScript-only)

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.