Skip to content

Commit 11f2216

Browse files
committed
Added section on Names
1 parent 0713d34 commit 11f2216

5 files changed

+111
-22
lines changed
2.84 KB
Binary file not shown.
19.3 KB
Binary file not shown.
2.66 KB
Binary file not shown.
12.6 KB
Binary file not shown.

doc/spec.md

Lines changed: 111 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ TypeScript is a trademark of Microsoft Corporation.
2727
* [1.10 Modules](#1.10)
2828
* [2 Basic Concepts](#2)
2929
* [2.1 Grammar Conventions](#2.1)
30-
* [2.2 Reserved Words](#2.2)
30+
* [2.2 Names](#2.2)
31+
* [2.2.1 Reserved Words](#2.2.1)
32+
* [2.2.2 Property Names](#2.2.2)
33+
* [2.2.3 Computed Property Names](#2.2.3)
3134
* [2.3 Declarations](#2.3)
3235
* [2.4 Scopes](#2.4)
33-
* [2.5 Computed Names and Symbols](#2.5)
3436
* [3 Types](#3)
3537
* [3.1 The Any Type](#3.1)
3638
* [3.2 Primitive Types](#3.2)
@@ -178,6 +180,7 @@ TypeScript is a trademark of Microsoft Corporation.
178180
* [8.4.1 Member Variable Declarations](#8.4.1)
179181
* [8.4.2 Member Function Declarations](#8.4.2)
180182
* [8.4.3 Member Accessor Declarations](#8.4.3)
183+
* [8.4.4 Dynamic Property Declarations](#8.4.4)
181184
* [8.5 Index Member Declarations](#8.5)
182185
* [8.6 Code Generation](#8.6)
183186
* [8.6.1 Classes Without Extends Clauses](#8.6.1)
@@ -786,9 +789,94 @@ The '*( Modified )*' annotation indicates that an existing grammar production is
786789

787790
Similar to the ECMAScript grammar, if the phrase "*[no LineTerminator here]*" appears in the right-hand side of a production of the syntactic grammar, it indicates that the production is not a match if a *LineTerminator* occurs in the input stream at the indicated position.
788791

789-
## <a name="2.2"/>2.2 Reserved Words
792+
## <a name="2.2"/>2.2 Names
790793

791-
*TODO: Include [list of reserved words](https://github.com/Microsoft/TypeScript/issues/2536)*.
794+
A core purpose of the TypeScript compiler is to track the named entities in a program and validate that they are used according to their designated meaning. Names in TypeScript can be written in several ways, depending on context. Specifically, a name can be written as
795+
796+
* an *IdentifierName*,
797+
* a *StringLiteral* in a property name,
798+
* a *NumericLiteral* in a property name, or
799+
* a *ComputedPropertyName* that denotes a well-known symbol ([2.2.3](#2.2.3)).
800+
801+
Most commonly, names are written to conform with the *Identifier* production, which is any *IdentifierName* that isn't a reserved word.
802+
803+
### <a name="2.2.1"/>2.2.1 Reserved Words
804+
805+
The following keywords are reserved and cannot be used as an *Identifier*:
806+
807+
```TypeScript
808+
break case catch class
809+
const continue debugger default
810+
delete do else enum
811+
export extends false finally
812+
for function if import
813+
in instanceof new null
814+
return super switch this
815+
throw true try typeof
816+
var void while with
817+
```
818+
819+
The following keywords cannot be used as identifiers in strict mode code, but are otherwise not restricted:
820+
821+
```TypeScript
822+
implements interface let package
823+
private protected public static
824+
yield
825+
```
826+
827+
The following keywords cannot be used as user defined type names, but are otherwise not restricted:
828+
829+
```TypeScript
830+
any boolean number string
831+
symbol
832+
```
833+
834+
The following keywords have special meaning in certain contexts, but are valid identifiers:
835+
836+
```TypeScript
837+
abstract as async await
838+
constructor declare from get
839+
is module namespace of
840+
require set type
841+
```
842+
843+
### <a name="2.2.2"/>2.2.2 Property Names
844+
845+
The *PropertyName* production from the ECMAScript grammar is reproduced below:
846+
847+
&emsp;&emsp;*PropertyName:*
848+
&emsp;&emsp;&emsp;*LiteralPropertyName*
849+
&emsp;&emsp;&emsp;*ComputedPropertyName*
850+
851+
&emsp;&emsp;*LiteralPropertyName:*
852+
&emsp;&emsp;&emsp;*IdentifierName*
853+
&emsp;&emsp;&emsp;*StringLiteral*
854+
&emsp;&emsp;&emsp;*NumericLiteral*
855+
856+
&emsp;&emsp;*ComputedPropertyName:*
857+
&emsp;&emsp;&emsp;`[`&emsp;*AssignmentExpression*&emsp;`]`
858+
859+
A property name can be any identifier (including a reserved word), a string literal, a numeric literal, or a computed property name. String literals may be used to give properties names that are not valid identifiers, such as names containing blanks. Numeric literal property names are equivalent to string literal property names with the string representation of the numeric literal, as defined in the ECMAScript specification.
860+
861+
### <a name="2.2.3"/>2.2.3 Computed Property Names
862+
863+
ECMAScript 6 permits object literals and classes to declare members with computed property names. A computed property name specifies an expression that computes the actual property name at run-time. Because the final property name isn't known at compile-time, TypeScript can only perform limited checks for entities declared with computed property names. However, a subset of computed property names known as ***well-known symbols*** can be used anywhere a *PropertyName* is expected, including property names within types. A computed property name is a well-known symbol if it is of the form
864+
865+
```TypeScript
866+
[ Symbol . xxx ]
867+
```
868+
869+
In a well-known symbol, the identifier to the right of the dot must denote a property of the primitive type `symbol` in the type of the global variable 'Symbol', or otherwise an error occurs.
870+
871+
In a *PropertyName* that specifies a *ComputedPropertyName*, the computed property name is required to denote a well-known symbol unless the property name occurs in a property assignment of an object literal ([4.5](#4.5)) or a property member declaration in a non-ambient class ([8.4](#8.4)).
872+
873+
Below is an example of an interface that declares a property with a well-known symbol name:
874+
875+
```TypeScript
876+
interface Iterable<T> {
877+
[Symbol.iterator](): Iterator<T>;
878+
}
879+
```
792880
793881
## <a name="2.3"/>2.3 Declarations
794882
@@ -921,14 +1009,6 @@ namespace M {
9211009
}
9221010
```
9231011

924-
## <a name="2.5"/>2.5 Computed Names and Symbols
925-
926-
*TODO: [Computed names](https://github.com/Microsoft/TypeScript/issues/1082) and [Symbols](https://github.com/Microsoft/TypeScript/pull/1978)*.
927-
928-
Computed names of the form `[Symbol.XXX]` are called well known symbols. Well known symbols are treated like special identifiers.
929-
930-
A declaration that specifies a computed name that isn't well known doesn't introduce a property in its containing type. However, if the containing type has an indexer, the type of the property will be included in the union type for the indexer.
931-
9321012
<br/>
9331013

9341014
# <a name="3"/>3 Types
@@ -1707,7 +1787,7 @@ Here, 'g' and 'g.x' have the same recursive type, and likewise 'h' and 'h()' hav
17071787

17081788
## <a name="3.9"/>3.9 Specifying Members
17091789

1710-
The members of an object type literal (section [3.8.3](#3.8.3)) are specified as a combination of property, call, construct, index, and method signatures.
1790+
The members of an object type literal (section [3.8.3](#3.8.3)) are specified as a combination of property, call, construct, index, and method signatures.
17111791

17121792
### <a name="3.9.1"/>3.9.1 Property Signatures
17131793

@@ -1716,17 +1796,10 @@ A property signature declares the name and type of a property member.
17161796
&emsp;&emsp;*PropertySignature:*
17171797
&emsp;&emsp;&emsp;*PropertyName*&emsp;`?`*<sub>opt</sub>*&emsp;*TypeAnnotation<sub>opt</sub>*
17181798

1719-
&emsp;&emsp;*PropertyName:*
1720-
&emsp;&emsp;&emsp;*IdentifierName*
1721-
&emsp;&emsp;&emsp;*StringLiteral*
1722-
&emsp;&emsp;&emsp;*NumericLiteral*
1723-
17241799
&emsp;&emsp;*TypeAnnotation:*
17251800
&emsp;&emsp;&emsp;`:`&emsp;*Type*
17261801

1727-
The *PropertyName* production, reproduced above from the ECMAScript grammar, permits a property name to be any identifier (including a reserved word), a string literal, or a numeric literal. String literals can be used to give properties names that are not valid identifiers, such as names containing blanks. Numeric literal property names are equivalent to string literal property names with the string representation of the numeric literal, as defined in the ECMAScript specification.
1728-
1729-
The *PropertyName* of a property signature must be unique within its containing type. If the property name is followed by a question mark, the property is optional. Otherwise, the property is required.
1802+
The *PropertyName* ([2.2.2](#2.2.2)) of a property signature must be unique within its containing type, and must denote a well-known symbol if it is a computed property name ([2.2.3](#2.2.3)). If the property name is followed by a question mark, the property is optional. Otherwise, the property is required.
17301803

17311804
If a property signature omits a *TypeAnnotation*, the Any type is assumed.
17321805

@@ -1903,7 +1976,7 @@ A method signature is shorthand for declaring a property of a function type.
19031976
&emsp;&emsp;*MethodSignature:*
19041977
&emsp;&emsp;&emsp;*PropertyName*&emsp;`?`*<sub>opt</sub>*&emsp;*CallSignature*
19051978

1906-
If the identifier is followed by a question mark, the property is optional. Otherwise, the property is required. Only object type literals and interfaces can declare optional properties.
1979+
If the *PropertyName* is a computed property name ([2.2.3](#2.2.3)), it must specify a well-known symbol. If the *PropertyName* is followed by a question mark, the property is optional. Otherwise, the property is required. Only object type literals and interfaces can declare optional properties.
19071980

19081981
A method signature of the form
19091982

@@ -2393,6 +2466,12 @@ If a get accessor is declared for a property, the return type of the get accesso
23932466
23942467
When an object literal is contextually typed by a type that includes a string index signature, the resulting type of the object literal includes a string index signature with the union type of the types of the properties declared in the object literal, or the Undefined type if the object literal is empty. Likewise, when an object literal is contextually typed by a type that includes a numeric index signature, the resulting type of the object literal includes a numeric index signature with the union type of the types of the numerically named properties (section [3.9.4](#3.9.4)) declared in the object literal, or the Undefined type if the object literal declares no numerically named properties.
23952468
2469+
If the *PropertyName* of a property assignment is a computed property name that doesn't denote a well-known symbol ([2.2.3](#2.2.3)), the construct is considered a ***dynamic property assignment***. The following rules apply to dynamic property assignments:
2470+
2471+
* A dynamic property assignment does not introduce a property in the type of the object literal.
2472+
* The property name expression of a dynamic property assignment must be of type Any or the String, Number, or Symbol primitive type.
2473+
* The name associated with a dynamic property assignment is considered to be a numeric property name if the property name expression is of type Any or the Number primitive type.
2474+
23962475
## <a name="4.6"/>4.6 Array Literals
23972476
23982477
An array literal
@@ -4546,6 +4625,14 @@ A static member accessor declaration declares a property in the constructor func
45464625
45474626
Get and set accessors are emitted as calls to 'Object.defineProperty' in the generated JavaScript, as described in section [8.6.1](#8.6.1).
45484627
4628+
### <a name="8.4.4"/>8.4.4 Dynamic Property Declarations
4629+
4630+
If the *PropertyName* of a property member declaration is a computed property name that doesn't denote a well-known symbol ([2.2.3](#2.2.3)), the construct is considered a ***dynamic property declaration***. The following rules apply to dynamic property declarations:
4631+
4632+
* A dynamic property declaration does not introduce a property in the class instance type or constructor function type.
4633+
* The property name expression of a dynamic property assignment must be of type Any or the String, Number, or Symbol primitive type.
4634+
* The name associated with a dynamic property declarations is considered to be a numeric property name if the property name expression is of type Any or the Number primitive type.
4635+
45494636
## <a name="8.5"/>8.5 Index Member Declarations
45504637
45514638
An index member declaration introduces an index signature (section [3.9.4](#3.9.4)) in the class instance type.
@@ -4805,6 +4892,8 @@ The body of an enum declaration defines zero or more enum members which are the
48054892
&emsp;&emsp;*EnumValue:*
48064893
&emsp;&emsp;&emsp;*AssignmentExpression*
48074894
4895+
The *PropertyName* of an enum member cannot be a computed property name ([2.2.3](#2.2.3)).
4896+
48084897
Enum members are either ***constant members*** or ***computed members***. Constant members have known constant values that are substituted in place of references to the members in the generated JavaScript code. Computed members have values that are computed at run-time and not known at compile-time. No substitution is performed for references to computed members.
48094898
48104899
An enum member is classified as follows:

0 commit comments

Comments
 (0)