π Edit on GitHub
Interfaces vs Types aliases
See the Type Aliases and Interfaces sections.
See blog post.
Examples from docs
Extending
Extend an interface
interface Animal {
name: string
}
interface Bear extends Animal {
honey: boolean
}
const bear = getBear()
bear.name
bear.honey
Extend a type via intersections
type Animal = {
name: string
}
type Bear = Animal & {
honey: Boolean
}
const bear = getBear()
bear.name
bear.honey
Combine two types.
type Name = {
name: "string"
};
type Age = {
age: number
};
type Person = Name & Age;
Combine two interfaces. You canβt reverse this as an interface made of two types.
interface Name {
name: βstringβ
};
interface Age {
age: number
};
type Person = Name & Age;
Adding fields
Declare an interface multiple types and they will all be collapsed into one.
interface Window {
title: string
}
interface Window {
ts: import("typescript")
}
const src = 'const a = "Hello World"';
window.ts.transpileModule(src, {});
You get an error if you do that with types.
// Error: Duplicate identifier 'Window'.