You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/spec.md
+13-7Lines changed: 13 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1867,7 +1867,7 @@ A type alias declaration introduces a ***type alias*** in the containing module.
1867
1867
1868
1868
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.
1869
1869
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.
1871
1871
1872
1872
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.
1873
1873
@@ -1894,24 +1894,30 @@ type RecFunc = () => RecFunc;
1894
1894
typeObjectStatics=typeofObject;
1895
1895
```
1896
1896
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
1898
1898
1899
1899
```TypeScript
1900
-
typePoint= { // Error
1900
+
interfacePoint{
1901
1901
x:number;
1902
1902
y:number;
1903
-
};
1903
+
}
1904
1904
```
1905
1905
1906
-
Such types must instead be written as interface declarations:
1906
+
could be written as the type alias
1907
1907
1908
1908
```TypeScript
1909
-
interfacePoint {
1909
+
typePoint= {
1910
1910
x:number;
1911
1911
y:number;
1912
-
}
1912
+
};
1913
1913
```
1914
1914
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
+
1915
1921
## <a name="3.10"/>3.10 Type Relationships
1916
1922
1917
1923
Types in TypeScript have identity, subtype, supertype, and assignment compatibility relationships as defined in the following sections.
0 commit comments