Suggest an editImprove this articleRefine the answer for “What does the type keyword do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)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. **Key point:** this works only at compile time and does not create a new data type in JS.Shown above the full answer for quick recall.Answer (EN)Image### 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` | Feature | `type` | `interface` | |---|---|---| | Can describe objects | yes | yes | | Can combine via `&` | yes | no (only extends) | | Can describe unions (`A \| B`) | yes | no | | Can describe primitives, functions, tuples, etc. | yes | no | | Can be extended | only via `&` | yes | | Can be redeclared and merged | no | yes | 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 | Property | Description | |---|---| | Purpose | Creating custom type aliases | | Works at the level of | Compilation (does not exist in JS) | | Supports | Primitives, functions, objects, unions, intersections | | Used for | Code reuse and readability | | JS equivalent | None (this is TypeScript-only) |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.