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
+48-51
Original file line number
Diff line number
Diff line change
@@ -1349,16 +1349,33 @@ class G<T> { // Introduce type parameter T
1349
1349
Types are specified either by referencing their keyword or name, or by writing object type literals, array type literals, tuple type literals, function type literals, constructor type literals, or type queries.
1350
1350
1351
1351
  *Type:*
1352
+
   *PrimaryOrUnionType*
1353
+
   *FunctionType*
1354
+
   *ConstructorType*
1355
+
1356
+
  *PrimaryOrUnionType:*
1357
+
   *PrimaryType*
1358
+
   *UnionType*
1359
+
1360
+
  *PrimaryType:*
1361
+
   *ParenthesizedType*
1352
1362
   *PredefinedType*
1353
1363
   *TypeReference*
1354
1364
   *ObjectType*
1355
1365
   *ArrayType*
1356
1366
   *TupleType*
1357
-
   *UnionType*
1358
-
   *FunctionType*
1359
-
   *ConstructorType*
1360
1367
   *TypeQuery*
1361
1368
1369
+
  *ParenthesizedType:*
1370
+
   `(` *Type* `)`
1371
+
1372
+
Parentheses are required around union, function, or constructor types when they are used as array element types, and parentheses are required around function or constructor types in union types. For example:
1373
+
1374
+
```TypeScript
1375
+
(string|number)[]
1376
+
((x:string) =>string) | (x:number) =>number)
1377
+
```
1378
+
1362
1379
The different forms of type notations are described in the following sections.
1363
1380
1364
1381
### <aname="3.6.1"/>3.6.1 Predefined Types
@@ -1461,36 +1478,24 @@ The members of an object type literal are specified as a combination of property
1461
1478
An array type literal is written as an element type followed by an open and close square bracket.
An array type literal references an array type (section [3.3.2](#3.3.2)) with the given element type. An array type literal is simply shorthand notation for a reference to the generic interface type 'Array' in the global module with the element type as a type argument.
1475
1484
1476
-
In order to avoid grammar ambiguities, array type literals permit only a restricted set of notations for the element type. Specifically, an *ArrayType* cannot start with a *UnionType*, *FunctionType*or *ConstructorType*. To use one of those forms for the element type, an array type must be written using the 'Array<T>' notation. For example, the type
1485
+
When union, function, or constructor types are used as array element types they must be enclosed in parentheses. For example:
1477
1486
1478
1487
```TypeScript
1479
-
() =>string[]
1488
+
(string|number)[]
1489
+
(() =>string))[]
1480
1490
```
1481
1491
1482
-
denotes a function returning a string array, not an array of functions returning string. The latter can be expressed using 'Array<T>' notation
1492
+
Alternatively, array types can be written using the 'Array<T>' notation. For example, the types above are equivalent to
1483
1493
1484
1494
```TypeScript
1495
+
Array<string|number>
1485
1496
Array<() =>string>
1486
1497
```
1487
1498
1488
-
or by writing the element type as an object type literal
1489
-
1490
-
```TypeScript
1491
-
{ ():string }[]
1492
-
```
1493
-
1494
1499
### <aname="3.6.5"/>3.6.5 Tuple Type Literals
1495
1500
1496
1501
A tuple type literal is written as a sequence of element types, separated by commas and enclosed in square brackets.
@@ -1512,28 +1517,22 @@ A tuple type literal references a tuple type (section [3.3.3](#3.3.3)).
1512
1517
A union type literal is written as a sequence of types separated by vertical bars.
A union typle literal references a union type (section [3.3.4](#3.3.4)).
1522
1523
1523
-
In order to avoid grammar ambiguities, union type literals permit only a restricted set of notations for the element types. Specifically, an element of a *UnionType* cannot be written as a *FunctionType* or *ConstructorType*. To include function or constructor types in a union type the function or constructor types must be written as object type literals. For example
1524
+
When function or constructor types are included in union types they must be enclosed in parentheses. For example:
1524
1525
1525
1526
```TypeScript
1526
-
() =>string| () =>number
1527
+
((x:string) =>string)| ((x:number) =>number)
1527
1528
```
1528
1529
1529
-
denotes a function whose return value is either a string or a function returning number, whereas
1530
+
Alternatively, function or constructor types in union types can be written using object literals:
1530
1531
1531
1532
```TypeScript
1532
-
{ ():string } | { ():number }
1533
+
{ (x:string):string } | { (x:number):number }
1533
1534
```
1534
1535
1535
-
denotes either a function returning string or a function returning number.
1536
-
1537
1536
### <a name="3.6.7"/>3.6.7 Function Type Literals
1538
1537
1539
1538
A function type literal specifies the type parameters, regular parameters, and return type of a call signature.
@@ -2221,8 +2220,8 @@ The resulting type an array literal expression is determined as follows:
2221
2220
The rules above mean that an array literal is always of an array type, unless it is contextually typed by a type with numerically named properties (such as a tuple type). For example
2222
2221
2223
2222
```TypeScript
2224
-
var a = [1, 2]; //Array<number>
2225
-
var b = ["hello", true]; //Array<string | boolean>
2223
+
var a = [1, 2]; // number[]
2224
+
var b = ["hello", true]; //(string | boolean)[]
2226
2225
var c: [number, string] = [3, "three"]; // [number, string]
2227
2226
```
2228
2227
@@ -5170,16 +5169,26 @@ This appendix contains a summary of the grammar found in the main document. As d
5170
5169
   *Type*
5171
5170
5172
5171
  *Type:*
5172
+
   *PrimaryOrUnionType*
5173
+
   *FunctionType*
5174
+
   *ConstructorType*
5175
+
5176
+
  *PrimaryOrUnionType:*
5177
+
   *PrimaryType*
5178
+
   *UnionType*
5179
+
5180
+
  *PrimaryType:*
5181
+
   *ParenthesizedType*
5173
5182
   *PredefinedType*
5174
5183
   *TypeReference*
5175
5184
   *ObjectType*
5176
5185
   *ArrayType*
5177
5186
   *TupleType*
5178
-
   *UnionType*
5179
-
   *FunctionType*
5180
-
   *ConstructorType*
5181
5187
   *TypeQuery*
5182
5188
5189
+
  *ParenthesizedType:*
5190
+
   `(` *Type* `)`
5191
+
5183
5192
  *PredefinedType:*
5184
5193
   `any`
5185
5194
   `number`
@@ -5216,15 +5225,7 @@ This appendix contains a summary of the grammar found in the main document. As d
0 commit comments