Types vs Interfaces
interface
vs type
¶
[!Rule of thumb: which to use: interface vs type]- use types unless you need a specific feature of interfaces
[!Why?]- interface: declaration merging if you redefine an interface somewhere else, it just extends it causes a bug
Interface vs type: not gonna be the TypeScript compiler bottleneck
[!What only
interface
can do]- - declaration merging - interfaces can be changed after being createdinterface Point { x: number; } interface Point { y: number; } //becomes: interface Point { x: number; y: number; }
[!What only
type
can do]- - primitives -type Name = string
- unions -type Id = string | number
- tuples -type Data = [number, string]
Last update:
2023-04-24