Sub topic 6
Interfaces and Type Aliases Are the Same to the Checker
Assume you know basic TypeScript syntax, including type annotations, interfaces, type aliases, objects, and functions. This chapter focuses on one rule: TypeScript usually compares the structure of a type, not whether the type came from an interface declaration or a type alias.
Two ways to describe an object
An interface gives a name to an object shape:
interface User {
id: number;
name: string;
}A type alias can describe the same shape:
type Account = {
id: number;
name: string;
};User and Account have different names, but they describe the same required members. A value that matches one can be assigned to the other.
const user: User = {
id: 1,
name: "Mira",
};
const account: Account = user;
const anotherUser: User = account;The checker does not ask whether user was created from the User declaration. It asks whether the value has an id number and a name string.
Assignability is based on members
Assignability means that a value of one type can be used where another type is expected. The target type lists the members that the value must provide.
interface HasName {
name: string;
}
type PersonRecord = {
name: string;
age: number;
};
function printName(value: HasName): void {
console.log(value.name);
}
const person: PersonRecord = {
name: "Ada",
age: 36,
};
printName(person);PersonRecord has the required name property, so it is assignable to HasName. Its extra age property does not prevent the assignment because a variable can contain more properties than the target requires.
The reverse direction also works only when the required shape is present:
interface HasName {
name: string;
}
type NameOnly = {
name: string;
};
const value: NameOnly = { name: "Sam" };
const named: HasName = value;A missing required member still causes an error, regardless of the declaration style:
type HasId = {
id: number;
};
interface HasLabel {
label: string;
}
// This would be invalid:
// const bad: HasId = { label: "missing id" };| Declaration form | Can describe | Typical strength |
|---|---|---|
| `interface` | Object and class instance shapes | Declaration merging and `extends` |
| `type` alias | Objects, unions, intersections, tuples, and primitives | Flexible type composition |
Where the syntax differs
Interfaces are designed primarily for named object contracts. They can extend other object types:
interface Timestamped {
createdAt: Date;
}
interface Message extends Timestamped {
text: string;
}Type aliases can express the same combined shape with an intersection. An intersection type combines members from multiple types.
type Timestamped = {
createdAt: Date;
};
type Message = Timestamped & {
text: string;
};
const message: Message = {
createdAt: new Date(),
text: "Hello",
};The declarations use different syntax, but Message still has the same effective members: createdAt and text.
Type aliases can also name unions and tuples, which interfaces cannot express directly:
type Status = "draft" | "published";
type Pair = [string, number];This difference affects what you can write, but it does not change the structural rule for compatible object shapes.
Declaration merging is an interface feature
An interface can be declared more than once. TypeScript merges the declarations:
interface Settings {
theme: "light" | "dark";
}
interface Settings {
language: string;
}
const settings: Settings = {
theme: "dark",
language: "en",
};A type alias cannot be redeclared with the same name in the same scope. This is one practical reason to choose an interface for an object contract that a library may extend.
When no merging or special syntax is needed, choose the form that communicates intent and matches the surrounding code. The assignability behavior remains structural.
Exercise
Why can a value typed as type NameOnly = { name: string } be assigned to interface HasName { name: string }?
Show answer
TypeScript compares the members required by the target. Both types require a name property of type string, so their object shapes are compatible.