Skip to content

Commit 2094f0f

Browse files
committed
Remove restriction on type aliases for object type literals
1 parent f35ad6b commit 2094f0f

5 files changed

+13
-7
lines changed
304 Bytes
Binary file not shown.
1.08 KB
Binary file not shown.
-4.14 KB
Binary file not shown.
712 Bytes
Binary file not shown.

doc/spec.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ A type alias declaration introduces a ***type alias*** in the containing module.
18671867
18681868
A type alias serves as an alias for the type specified in the type alias declaration. Unlike an interface declaration, which always introduces a named object type, a type alias declaration can introduce a name for any kind of type, including primitive types and union types.
18691869
1870-
Type aliases are referenced using type references ([3.7.2](#3.7.2)). Writing a reference to a type alias has exactly the same effect as writing the aliased type itself.
1870+
Type aliases are referenced using type references ([3.7.2](#3.7.2)). Writing a reference to a type alias has ***exactly*** the same effect as writing the aliased type itself.
18711871
18721872
The *Identifier* of a type alias declaration may not be one of the predefined type names (section [3.7.1](#3.7.1)). Furthermore, the *Type* of a type alias may not be an object type literal (section [3.7.3](#3.7.3)) or a parenthesized form of an object type literal, but any other kind of type composed from an object type literal is permitted.
18731873
@@ -1894,24 +1894,30 @@ type RecFunc = () => RecFunc;
18941894
type ObjectStatics = typeof Object;
18951895
```
18961896
1897-
To ensure that named object types are not inadvertently introduced as aliases for anonymous object type literals, a type alias is not permitted to alias an object type literal. For example, the following is an error:
1897+
Interface types have many similarities to type aliases for object type literals, but since interface types offer more capabilities they are generally preferred to type aliases. For example, the interface type
18981898
18991899
```TypeScript
1900-
type Point = { // Error
1900+
interface Point {
19011901
x: number;
19021902
y: number;
1903-
};
1903+
}
19041904
```
19051905
1906-
Such types must instead be written as interface declarations:
1906+
could be written as the type alias
19071907
19081908
```TypeScript
1909-
interface Point {
1909+
type Point = {
19101910
x: number;
19111911
y: number;
1912-
}
1912+
};
19131913
```
19141914
1915+
However, doing so means the following capabilities are lost:
1916+
1917+
* An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
1918+
* An interface can have multiple merged declarations, but a type alias for an object type literal cannot.
1919+
* An interface is referenced by its name in error messages and tooling, but a type alias is always expanded to its structural representation.
1920+
19151921
## <a name="3.10"/>3.10 Type Relationships
19161922
19171923
Types in TypeScript have identity, subtype, supertype, and assignment compatibility relationships as defined in the following sections.

0 commit comments

Comments
 (0)