-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type alias = {} question #1945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
No. Interfaces may only extend named object types. They can't extend primitives or anonymous object types. In this case, See #957 (comment) (mostly that comment, but also ones above it) for an extended discussion of how |
The latest copy of the language spec has some explanation of the difference between interfaces and type aliases: 3.9 interface Point {
x: number;
y: number;
} could be written as the type alias type Point = {
x: number;
y: number;
}; However, doing so means the following capabilities are lost: |
I believe the re-opening of type definitions was one of the primary reasons to not re-use interface as is. It seems a bit subtle to say re: ItemApi2, it does look a little awkward but remember that the type ItemApi2 = {
items: Item[];
addItem: () => void;
} and that doesn't look so strange. One use case to remember is people often want to alias function types, but these are ultimately types with bodies. That is, this type Callback = (data: string) => void; seems nice to have (even more so if/when aliases allow generic parameters). People already used interfaces for this purpose (ex underscore/lodash.d.ts), but that necessarily allows your alias to be re-opened to include other members. In reality the type alias means: type Callback = { (data: string): void; } So if you don't allow types with bodies on the RHS then you can't alias function types. We definitely went back and forth on the whole thing for awhile, considering whether/if to re-use |
looks like there is no actions/information required from this issue at this point. closing. |
If a type is an alias for {}, should an interface be allowed to extend it?
The text was updated successfully, but these errors were encountered: