Skip to content

Commit 152a940

Browse files
committed
Merge pull request #914 from Microsoft/parensInTypes
Parentheses in type literals
2 parents e96983d + f3bd294 commit 152a940

File tree

73 files changed

+1702
-1478
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1702
-1478
lines changed

Jakefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ var word2mdJs = path.join(scriptsDirectory, "word2md.js");
279279
var word2mdTs = path.join(scriptsDirectory, "word2md.ts");
280280
var specWord = path.join(docDirectory, "TypeScript Language Specification.docx");
281281
var specMd = path.join(docDirectory, "spec.md");
282-
var headerMd = path.join(docDirectory, "header.md");
283282

284283
file(word2mdTs);
285284

@@ -292,7 +291,6 @@ compileFile(word2mdJs,
292291

293292
// The generated spec.md; built for the 'generate-spec' task
294293
file(specMd, [word2mdJs, specWord], function () {
295-
jake.cpR(headerMd, specMd, {silent: true});
296294
var specWordFullPath = path.resolve(specWord);
297295
var cmd = "cscript //nologo " + word2mdJs + ' "' + specWordFullPath + '" ' + specMd;
298296
console.log(cmd);
Binary file not shown.
Binary file not shown.
11.4 KB
Binary file not shown.
-1.38 KB
Binary file not shown.

doc/header.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

doc/spec.md

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,8 +1155,8 @@ For purposes of property access (section [4.10](#4.10)) and function calls ([4.1
11551155
When used as a contextual type (section [4.19](#4.19)), a union type *U* has those members that are present in any of its constituent types, with types that are unions of the respective members in the constituent types. Specifically:
11561156

11571157
* Let *S* be the set of types in *U* that has a property *P*. If *S* is not empty, *U* has a property *P* of a union type of the types of *P* from each type in *S*.
1158-
* Let *S* be the set of types in *U* that have call signatures. If *S* is not empty and the sets of call signatures of the types in *S* are identical ignoring return types,* U* has the same set of call signatures, but with return types that are unions of the return types of the respective call signatures from each type in *S*.
1159-
* Let *S* be the set of types in *U* that have construct signatures. If *S* is not empty and the sets of construct signatures of the types in *S* are identical ignoring return types,* U* has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in *S*.
1158+
* Let *S* be the set of types in *U* that have call signatures. If *S* is not empty and the sets of call signatures of the types in *S* are identical ignoring return types, *U* has the same set of call signatures, but with return types that are unions of the return types of the respective call signatures from each type in *S*.
1159+
* Let *S* be the set of types in *U* that have construct signatures. If *S* is not empty and the sets of construct signatures of the types in *S* are identical ignoring return types, *U* has the same set of construct signatures, but with return types that are unions of the return types of the respective construct signatures from each type in *S*.
11601160
* Let *S* be the set of types in *U* that has a string index signature. If *S* is not empty, *U* has a string index signature of a union type of the types of the string index signatures from each type in *S*.
11611161
* Let *S* be the set of types in *U* that has a numeric index signature. If *S* is not empty, *U* has a numeric index signature of a union type of the types of the numeric index signatures from each type in *S*.
11621162

@@ -1349,16 +1349,33 @@ class G<T> { // Introduce type parameter T
13491349
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.
13501350

13511351
&emsp;&emsp;*Type:*
1352+
&emsp;&emsp;&emsp;*PrimaryOrUnionType*
1353+
&emsp;&emsp;&emsp;*FunctionType*
1354+
&emsp;&emsp;&emsp;*ConstructorType*
1355+
1356+
&emsp;&emsp;*PrimaryOrUnionType:*
1357+
&emsp;&emsp;&emsp;*PrimaryType*
1358+
&emsp;&emsp;&emsp;*UnionType*
1359+
1360+
&emsp;&emsp;*PrimaryType:*
1361+
&emsp;&emsp;&emsp;*ParenthesizedType*
13521362
&emsp;&emsp;&emsp;*PredefinedType*
13531363
&emsp;&emsp;&emsp;*TypeReference*
13541364
&emsp;&emsp;&emsp;*ObjectType*
13551365
&emsp;&emsp;&emsp;*ArrayType*
13561366
&emsp;&emsp;&emsp;*TupleType*
1357-
&emsp;&emsp;&emsp;*UnionType*
1358-
&emsp;&emsp;&emsp;*FunctionType*
1359-
&emsp;&emsp;&emsp;*ConstructorType*
13601367
&emsp;&emsp;&emsp;*TypeQuery*
13611368

1369+
&emsp;&emsp;*ParenthesizedType:*
1370+
&emsp;&emsp;&emsp;`(`&emsp;*Type*&emsp;`)`
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+
13621379
The different forms of type notations are described in the following sections.
13631380

13641381
### <a name="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
14611478
An array type literal is written as an element type followed by an open and close square bracket.
14621479

14631480
&emsp;&emsp;*ArrayType:*
1464-
&emsp;&emsp;&emsp;*ElementType*&emsp;*[no LineTerminator here]*&emsp;`[`&emsp;`]`
1465-
1466-
&emsp;&emsp;*ElementType:*
1467-
&emsp;&emsp;&emsp;*PredefinedType*
1468-
&emsp;&emsp;&emsp;*TypeReference*
1469-
&emsp;&emsp;&emsp;*ObjectType*
1470-
&emsp;&emsp;&emsp;*ArrayType*
1471-
&emsp;&emsp;&emsp;*TupleType*
1472-
&emsp;&emsp;&emsp;*TypeQuery*
1481+
&emsp;&emsp;&emsp;*PrimaryType*&emsp;*[no LineTerminator here]*&emsp;`[`&emsp;`]`
14731482

14741483
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.
14751484

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&lt;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:
14771486

14781487
```TypeScript
1479-
() => string[]
1488+
(string | number)[]
1489+
(() => string))[]
14801490
```
14811491

1482-
denotes a function returning a string array, not an array of functions returning string. The latter can be expressed using 'Array&lt;T>' notation
1492+
Alternatively, array types can be written using the 'Array&lt;T>' notation. For example, the types above are equivalent to
14831493

14841494
```TypeScript
1495+
Array<string | number>
14851496
Array<() => string>
14861497
```
14871498

1488-
or by writing the element type as an object type literal
1489-
1490-
```TypeScript
1491-
{ (): string }[]
1492-
```
1493-
14941499
### <a name="3.6.5"/>3.6.5 Tuple Type Literals
14951500

14961501
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)).
15121517
A union type literal is written as a sequence of types separated by vertical bars.
15131518

15141519
&emsp;&emsp;*UnionType:*
1515-
&emsp;&emsp;&emsp;*ElementType*&emsp;`|`&emsp;*UnionOrElementType*
1516-
1517-
&emsp;&emsp;*UnionOrElementType:*
1518-
&emsp;&emsp;&emsp;*UnionType*
1519-
&emsp;&emsp;&emsp;*ElementType*
1520+
&emsp;&emsp;&emsp;*PrimaryOrUnionType*&emsp;`|`&emsp;*PrimaryType*
15201521

15211522
A union typle literal references a union type (section [3.3.4](#3.3.4)).
15221523

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:
15241525

15251526
```TypeScript
1526-
() => string | () => number
1527+
((x: string) => string) | ((x: number) => number)
15271528
```
15281529

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:
15301531

15311532
```TypeScript
1532-
{ (): string } | { (): number }
1533+
{ (x: string): string } | { (x: number): number }
15331534
```
15341535
1535-
denotes either a function returning string or a function returning number.
1536-
15371536
### <a name="3.6.7"/>3.6.7 Function Type Literals
15381537
15391538
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:
22212220
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
22222221
22232222
```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)[]
22262225
var c: [number, string] = [3, "three"]; // [number, string]
22272226
```
22282227
@@ -5170,16 +5169,26 @@ This appendix contains a summary of the grammar found in the main document. As d
51705169
&emsp;&emsp;&emsp;*Type*
51715170
51725171
&emsp;&emsp;*Type:*
5172+
&emsp;&emsp;&emsp;*PrimaryOrUnionType*
5173+
&emsp;&emsp;&emsp;*FunctionType*
5174+
&emsp;&emsp;&emsp;*ConstructorType*
5175+
5176+
&emsp;&emsp;*PrimaryOrUnionType:*
5177+
&emsp;&emsp;&emsp;*PrimaryType*
5178+
&emsp;&emsp;&emsp;*UnionType*
5179+
5180+
&emsp;&emsp;*PrimaryType:*
5181+
&emsp;&emsp;&emsp;*ParenthesizedType*
51735182
&emsp;&emsp;&emsp;*PredefinedType*
51745183
&emsp;&emsp;&emsp;*TypeReference*
51755184
&emsp;&emsp;&emsp;*ObjectType*
51765185
&emsp;&emsp;&emsp;*ArrayType*
51775186
&emsp;&emsp;&emsp;*TupleType*
5178-
&emsp;&emsp;&emsp;*UnionType*
5179-
&emsp;&emsp;&emsp;*FunctionType*
5180-
&emsp;&emsp;&emsp;*ConstructorType*
51815187
&emsp;&emsp;&emsp;*TypeQuery*
51825188
5189+
&emsp;&emsp;*ParenthesizedType:*
5190+
&emsp;&emsp;&emsp;`(`&emsp;*Type*&emsp;`)`
5191+
51835192
&emsp;&emsp;*PredefinedType:*
51845193
&emsp;&emsp;&emsp;`any`
51855194
&emsp;&emsp;&emsp;`number`
@@ -5216,15 +5225,7 @@ This appendix contains a summary of the grammar found in the main document. As d
52165225
&emsp;&emsp;&emsp;*MethodSignature*
52175226
52185227
&emsp;&emsp;*ArrayType:*
5219-
&emsp;&emsp;&emsp;*ElementType*&emsp;*[no LineTerminator here]*&emsp;`[`&emsp;`]`
5220-
5221-
&emsp;&emsp;*ElementType:*
5222-
&emsp;&emsp;&emsp;*PredefinedType*
5223-
&emsp;&emsp;&emsp;*TypeReference*
5224-
&emsp;&emsp;&emsp;*ObjectType*
5225-
&emsp;&emsp;&emsp;*ArrayType*
5226-
&emsp;&emsp;&emsp;*TupleType*
5227-
&emsp;&emsp;&emsp;*TypeQuery*
5228+
&emsp;&emsp;&emsp;*PrimaryType*&emsp;*[no LineTerminator here]*&emsp;`[`&emsp;`]`
52285229
52295230
&emsp;&emsp;*TupleType:*
52305231
&emsp;&emsp;&emsp;`[`&emsp;*TupleElementTypes*&emsp;`]`
@@ -5237,11 +5238,7 @@ This appendix contains a summary of the grammar found in the main document. As d
52375238
&emsp;&emsp;&emsp;*Type*
52385239
52395240
&emsp;&emsp;*UnionType:*
5240-
&emsp;&emsp;&emsp;*ElementType*&emsp;`|`&emsp;*UnionOrElementType*
5241-
5242-
&emsp;&emsp;*UnionOrElementType:*
5243-
&emsp;&emsp;&emsp;*UnionType*
5244-
&emsp;&emsp;&emsp;*ElementType*
5241+
&emsp;&emsp;&emsp;*PrimaryOrUnionType*&emsp;`|`&emsp;*PrimaryType*
52455242
52465243
&emsp;&emsp;*FunctionType:*
52475244
&emsp;&emsp;&emsp;*TypeParameters<sub>opt</sub>*&emsp;`(`&emsp;*ParameterList<sub>opt</sub>*&emsp;`)`&emsp;`=>`&emsp;*Type*

0 commit comments

Comments
 (0)