Sub topic 6
Interfaces and type aliases are the same to the checker
This chapter assumes you can write TypeScript type annotations, declare interfaces and type aliases, and use JavaScript objects, classes, and functions. It also assumes you know the basic rule from earlier chapters: a value is assignable to a target type when its shape contains the target's required members.
The checker compares members, not declaration keywords
Developers often ask whether interface and type behave differently when you assign one value to another. For object shapes they don't. The assignability check resolves each type to its members (names, types, optionality, readonly flags, call signatures) and compares those. The keyword that introduced the type is not part of the comparison.
interface PointI {
x: number;
y: number;
}
type PointT = {
x: number;
y: number;
};
const a: PointI = { x: 1, y: 2 };
const b: PointT = a; // OK: same members
const c: PointI = b; // OK in the other direction too
function norm(p: PointT): number {
return Math.sqrt(p.x * p.x + p.y * p.y);
}
console.log(norm(a)); // 2.23606797749979The names PointI and PointT are labels for error messages and hover text. They carry no identity. This is the same idea as in the rest of the tutorial: names don't matter, shapes do.
Where else the two are interchangeable
Anywhere TypeScript expects an object type, either form works.
- Function parameters and return types. A function declared with a
PointIparameter accepts any value with a compatible shape, whether it was typed as an interface, an alias, a class instance, or an inferred object literal type. implementsclauses. A class can implement an interface or an alias of an object type. The check is a normal assignability check of the class instance type against the target.extendsclauses. An interface can extend another interface or an alias of an object type, as long as the alias resolves to an object type with statically known members.- Generic constraints.
T extends PointIandT extends PointTaccept exactly the same type arguments.
type Named = { name: string };
interface Aged {
age: number;
}
interface Person extends Named, Aged {}
class Employee implements Named, Aged {
constructor(public name: string, public age: number) {}
}
function describe<T extends Named & Aged>(v: T): string {
return `${v.name} (${v.age})`;
}
const p: Person = { name: "Ana", age: 30 };
console.log(describe(p));
console.log(describe(new Employee("Ben", 41)));Employee never mentions Person, yet it is accepted wherever a Person is expected, because it has the same members.
What the two forms do not share
The equivalence is about assignability. The declaration features differ. The differences below affect what you can write, and one of them (implicit index signatures) does change assignability.
| Feature | `interface` | `type` alias |
|---|---|---|
| Declare an object shape | Yes | Yes |
| Declaration merging (same name declared twice) | Yes, members are combined | No, duplicate identifier error |
| Name a union, tuple, or primitive | No | Yes |
| Mapped and conditional types | No | Yes |
| Combine with `extends` | Yes, with conflict checks | Uses `&` intersections instead |
| Implicit index signature | No | Yes, for object literal types |
Declaration merging
Two interfaces with the same name in the same scope merge into one type. An alias cannot do this.
interface Config {
host: string;
}
interface Config {
port: number;
}
const cfg: Config = { host: "localhost", port: 8080 }; // both members required
type Settings = { host: string };
// type Settings = { port: number };
// Error if uncommented: Duplicate identifier 'Settings'.Merging is why library authors prefer interfaces for types that users may need to augment, such as the global Window interface. For the checker, a merged interface is simply an object type with the combined members.
Aliases can name things interfaces cannot
An alias can name any type, not only object shapes.
type Id = string | number;
type Pair = [string, number];
type ReadonlyShape<T> = { readonly [K in keyof T]: T[K] };
const id: Id = "abc";
const pair: Pair = ["age", 7];
const frozen: ReadonlyShape<{ a: number }> = { a: 1 };
// frozen.a = 2; // Error: Cannot assign to 'a' because it is a read-only property.When an alias names a union, assignability follows the union rules (a value must be assignable to at least one member). That is a property of the union, not of the alias keyword.
`extends` versus intersection
Both combine shapes, but they react differently to conflicts.
interface A {
id: string;
}
// interface B extends A { id: number }
// Error if uncommented: Interface 'B' incorrectly extends interface 'A'.
type C = A & { id: number }; // allowed; the property 'id' has type 'never'An interface extends clause checks immediately that the new interface is assignable to the base, so a conflicting property is reported where you wrote it. An intersection is not checked for conflicts at the declaration. The conflict shows up later as a property of type never, which makes the resulting type impossible to satisfy. That makes extends the safer choice when you intend to refine a base type.
The one place assignability differs: implicit index signatures
A type alias for an object literal type is assignable to a target with an index signature when all its known properties match the index type. An interface is not, because interfaces can be merged later with more members, so TypeScript does not assume their full set of properties is closed.
interface UserI {
name: string;
}
type UserT = { name: string };
function printAll(record: Record<string, string>): void {
for (const key of Object.keys(record)) {
console.log(key, record[key]);
}
}
const ui: UserI = { name: "Ana" };
const ut: UserT = { name: "Ana" };
printAll(ut); // OK: aliases get an implicit index signature
printAll(ui);
// Error: Argument of type 'UserI' is not assignable to parameter of type
// 'Record<string, string>'.
// Index signature for type 'string' is missing in type 'UserI'.This is the most common surprise in practice. Code that worked with an alias breaks when someone converts the alias to an interface, and the other way around. The two types have the same named members. The difference is that only the alias counts as having a fixed, closed set of properties, which is what an index signature target needs.
There are three ways to handle it:
- Declare the type as an alias when it must flow into
Record<string, ...>parameters. - Add an explicit index signature to the interface, if the type really is open-ended:
[key: string]: string. - Change the target parameter to a generic constraint such as
<T extends { name: string }>so no index signature is needed.
Names still appear in diagnostics
The checker ignores names when comparing, but it uses them when it describes a type to you. Error messages and hover text show the alias or interface name where there is one, and expand the members when there is not. Interfaces keep their name in messages. Aliases of object literal types also usually print by name, but aliases built from intersections or mapped types may be expanded, which makes some messages long.
interface Shape {
kind: string;
area: number;
}
type Circle = { kind: string; radius: number };
const circle: Circle = { kind: "circle", radius: 2 };
const shape: Shape = circle;
// Error: Property 'area' is missing in type 'Circle' but required in type 'Shape'.The error lists the missing member (area). It names the types for readability, but the failure is purely structural.
Choosing between them
Because assignability is the same for ordinary object shapes, the choice rests on other criteria.
- Use
interfacefor object shapes that may be extended by other interfaces or augmented by consumers. Interfaces also produceextendsconflict errors early, and the compiler caches their resolved members by name, which helps in large codebases. - Use
typewhen you need unions, tuples, mapped or conditional types, or when a value must be assignable to an index signature target. - Pick one convention per codebase for plain object shapes, and note the exceptions above in a comment when you break it.
Do not choose based on a belief that interfaces are "nominal" or aliases are "structural". Both are structural. Only classes with private or protected members add nominal behavior, which is covered in a later chapter.
Summary of the working model
- The checker resolves both sides to members and compares them. The declaration keyword is not part of the comparison.
- Merging, unions, tuples, mapped types, and
extendsversus&are declaration-level differences. They change what you can write and when conflicts are reported. - The implicit index signature rule is the one place where an interface and an equivalent alias differ in assignability.
Exercise
Given interface Opts { verbose: boolean } and type OptsT = { verbose: boolean }, a function run(o: Record<string, boolean>) accepts a value typed OptsT. Does it accept a value typed Opts, and what are two ways to make it accept one?
Show answer
No. An interface has no implicit index signature, so TypeScript reports "Index signature for type 'string' is missing in type 'Opts'". You can fix it by declaring Opts as a type alias, or by adding an explicit index signature to the interface, for example [key: string]: boolean. Changing run to a generic like <T extends { verbose: boolean }>(o: T) also works.
Exercise
Two interfaces named Settings are declared in the same file, one with host: string and one with port: number. What is the type of a variable annotated Settings, and would the same code with two type Settings aliases compile?
Show answer
The interfaces merge, so Settings requires both host: string and port: number. Two type Settings aliases would not compile, because the compiler reports a duplicate identifier.