Menu

TypeScript Type Alias: The type Keyword with Examples

A type alias gives a name to any type with the type keyword: object shapes, unions, tuples, functions, generics and recursive types. Learn the syntax, what each form looks like, and why an alias is only a name and not a new, separate type.

This page includes runnable editors - edit, run, and see output instantly.

A type alias gives a name to a type. You write type Name = ..., and from then on Name can be used anywhere a type is expected. Unlike an interface, an alias can name any type at all, not only object shapes.

Aliases exist only at compile time. The emitted JavaScript contains none of these type lines.

Syntax

type Name = SomeType;
type Name<T> = SomeTypeUsing<T>;       // generic
export type Name = SomeType;           // exported from a module

Names are PascalCase by convention. The right-hand side is any type expression, and an alias can use other aliases, including itself (see recursive types below). The alias is available in the whole scope where it is declared, even above the declaration line, because types are checked at compile time and not executed in order like const or let.

Object Types

For an object shape, an alias and an interface are written almost the same way. The alias uses = and is usually ended with a semicolon.

An alias cannot use extends; the & intersection plays that role. Excess property checks, optional and readonly properties work exactly as they do for interfaces. Which of the two to pick for object shapes is covered on the interface vs type page.

Unions, Tuples and Functions

These are the cases only an alias can name, and the main reason type exists. A union alias lists the alternatives, a tuple alias fixes the length and element types of an array, and a function alias describes a signature.

In scale, the parameters need no annotations: the Transform alias supplies their types. Tuple syntax is covered in tuples, and union narrowing in the union types page.

Generic Type Aliases

Type parameters make one alias work for many types. They can have defaults, just like function parameters.

Result<number> uses the default E = string. Small generic aliases like type Nullable<T> = T | null or type Dict<T> = Record<string, T> are common in real code. The built-in utility types (Partial<T>, Pick<T, K>, ReturnType<F>) are generic aliases too, declared in the standard library.

Aliases Are Names, Not New Types

An alias does not create a distinct type. It is a second name for the type on the right, and the compiler treats the two as identical. Two aliases of string are fully interchangeable:

This prints cancelling u_42 with no error, which is the bug an alias cannot prevent. The alias still helps readers (a parameter typed OrderId says more than string), but if mixing up two ids must be a compile error, use a branded type such as string & { readonly __brand: "OrderId" }.

Recursive Type Aliases

An alias can refer to itself, which is how you describe trees, nested lists and JSON:

The Json alias rejects values that JSON cannot represent: { when: new Date() } or { f: undefined } assigned to Json are compile errors (TS2322).

Types from Values: typeof

When a value already exists, you can name its type instead of writing it out. The typeof type operator reads the type of a variable, and it combines with other operators:

const defaults = { retries: 3, verbose: false, level: "info" };

type Options = typeof defaults;
// { retries: number; verbose: boolean; level: string }

type OptionKey = keyof typeof defaults;
// "retries" | "verbose" | "level"

function start(port: number) {
  return { port, startedAt: new Date() };
}
type Server = ReturnType<typeof start>;
// { port: number; startedAt: Date }

The value stays the single source of truth: add a property to defaults and Options follows. More on this in typeof.

Exporting and Importing Aliases

Aliases are exported and imported like values. import type makes it explicit that only a type is imported, so the import is always removed from the JavaScript output:

// shapes.ts
export type Point = { x: number; y: number };
export type Shape = { kind: "circle"; center: Point; radius: number };

// main.ts
import type { Point, Shape } from "./shapes.js";

const p: Point = { x: 1, y: 2 };

Frequently Asked Questions

What is a type alias in TypeScript?

A type alias is a name for a type, declared with the type keyword: type Point = { x: number; y: number }. After that, Point can be used anywhere a type is expected. It can name any type: an object shape, a union, a tuple, a function signature, a primitive or a generic type.

Does a type alias create a new type?

No. An alias is only another name for an existing type. With type UserId = string and type OrderId = string, a UserId can be passed where an OrderId is expected, because both are just string. To make types that are not interchangeable, use a branded type.

Can a type alias refer to itself?

Yes, as long as the self-reference is nested inside an object type, an array or a tuple: type TreeNode = { value: number; children: TreeNode[] } and a Json alias that includes Json[] both work. A bare reference such as type Loop = Loop | string is error TS2456, Type alias 'Loop' circularly references itself.

Can a type alias be generic?

Yes. Put type parameters after the name: type Box<T> = { value: T }, then use it as Box<number>. Parameters can have defaults (type Result<T, E = string> = ...) and constraints (type Keys<T extends object> = keyof T).

How do I get a type from an existing object?

Use the typeof type operator: const defaults = { retries: 3, verbose: false }; type Options = typeof defaults; gives { retries: number; verbose: boolean }. This keeps one source of truth when the value comes first.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED