Files
TypeScript/doc/spec/Grammar.md

24 KiB
Raw Permalink Blame History

Grammar

This appendix contains a summary of the grammar found in the main document. As described in section [#grammar-conventions], the TypeScript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar.

Types

TypeParameters: <TypeParameterList>

TypeParameterList: TypeParameter TypeParameterList,TypeParameter

TypeParameter: BindingIdentifierConstraintopt

Constraint: extendsType

TypeArguments: <TypeArgumentList>

TypeArgumentList: TypeArgument TypeArgumentList,TypeArgument

TypeArgument: Type

Type: UnionOrIntersectionOrPrimaryType FunctionType ConstructorType

UnionOrIntersectionOrPrimaryType: UnionType IntersectionOrPrimaryType

IntersectionOrPrimaryType: IntersectionType PrimaryType

PrimaryType: ParenthesizedType PredefinedType TypeReference ObjectType ArrayType TupleType TypeQuery ThisType

ParenthesizedType: (Type)

PredefinedType: any number boolean string symbol void

TypeReference: TypeName[no LineTerminator here]TypeArgumentsopt

TypeName: IdentifierReference NamespaceName.IdentifierReference

NamespaceName: IdentifierReference NamespaceName.IdentifierReference

ObjectType: {TypeBodyopt}

TypeBody: TypeMemberList;opt TypeMemberList,opt

TypeMemberList: TypeMember TypeMemberList;TypeMember TypeMemberList,TypeMember

TypeMember: PropertySignature CallSignature ConstructSignature IndexSignature MethodSignature

ArrayType: PrimaryType[no LineTerminator here][]

TupleType: [TupleElementTypes]

TupleElementTypes: TupleElementType TupleElementTypes,TupleElementType

TupleElementType: Type

UnionType: UnionOrIntersectionOrPrimaryType|IntersectionOrPrimaryType

IntersectionType: IntersectionOrPrimaryType&PrimaryType

FunctionType: TypeParametersopt(ParameterListopt)=>Type

ConstructorType: newTypeParametersopt(ParameterListopt)=>Type

TypeQuery: typeofTypeQueryExpression

TypeQueryExpression: IdentifierReference TypeQueryExpression.IdentifierName

ThisType: this

PropertySignature: PropertyName?optTypeAnnotationopt

PropertyName: IdentifierName StringLiteral NumericLiteral

TypeAnnotation: :Type

CallSignature: TypeParametersopt(ParameterListopt)TypeAnnotationopt

ParameterList: RequiredParameterList OptionalParameterList RestParameter RequiredParameterList,OptionalParameterList RequiredParameterList,RestParameter OptionalParameterList,RestParameter RequiredParameterList,OptionalParameterList,RestParameter

RequiredParameterList: RequiredParameter RequiredParameterList,RequiredParameter

RequiredParameter: AccessibilityModifieroptBindingIdentifierOrPatternTypeAnnotationopt BindingIdentifier:StringLiteral

AccessibilityModifier: public private protected

BindingIdentifierOrPattern: BindingIdentifier BindingPattern

OptionalParameterList: OptionalParameter OptionalParameterList,OptionalParameter

OptionalParameter: AccessibilityModifieroptBindingIdentifierOrPattern?TypeAnnotationopt AccessibilityModifieroptBindingIdentifierOrPatternTypeAnnotationoptInitializer BindingIdentifier?:StringLiteral

RestParameter: ...BindingIdentifierTypeAnnotationopt

ConstructSignature: newTypeParametersopt(ParameterListopt)TypeAnnotationopt

IndexSignature: [BindingIdentifier:string]TypeAnnotation [BindingIdentifier:number]TypeAnnotation

MethodSignature: PropertyName?optCallSignature

TypeAliasDeclaration: typeBindingIdentifierTypeParametersopt=Type;

Expressions

PropertyDefinition: ( Modified ) IdentifierReference CoverInitializedName PropertyName:AssignmentExpression PropertyNameCallSignature{FunctionBody} GetAccessor SetAccessor

GetAccessor: getPropertyName()TypeAnnotationopt{FunctionBody}

SetAccessor: setPropertyName(BindingIdentifierOrPatternTypeAnnotationopt){FunctionBody}

FunctionExpression: ( Modified ) functionBindingIdentifieroptCallSignature{FunctionBody}

ArrowFormalParameters: ( Modified ) CallSignature

Arguments: ( Modified ) TypeArgumentsopt(ArgumentListopt)

UnaryExpression: ( Modified )  … <Type>UnaryExpression

Statements

Declaration: ( Modified )  … InterfaceDeclaration TypeAliasDeclaration EnumDeclaration

VariableDeclaration: ( Modified ) SimpleVariableDeclaration DestructuringVariableDeclaration

SimpleVariableDeclaration: BindingIdentifierTypeAnnotationoptInitializeropt

DestructuringVariableDeclaration: BindingPatternTypeAnnotationoptInitializer

LexicalBinding: ( Modified ) SimpleLexicalBinding DestructuringLexicalBinding

SimpleLexicalBinding: BindingIdentifierTypeAnnotationoptInitializeropt

DestructuringLexicalBinding: BindingPatternTypeAnnotationoptInitializeropt

Functions

FunctionDeclaration: ( Modified ) functionBindingIdentifieroptCallSignature{FunctionBody} functionBindingIdentifieroptCallSignature;

Interfaces

InterfaceDeclaration: interfaceBindingIdentifierTypeParametersoptInterfaceExtendsClauseoptObjectType

InterfaceExtendsClause: extendsClassOrInterfaceTypeList

ClassOrInterfaceTypeList: ClassOrInterfaceType ClassOrInterfaceTypeList,ClassOrInterfaceType

ClassOrInterfaceType: TypeReference

Classes

ClassDeclaration: ( Modified ) classBindingIdentifieroptTypeParametersoptClassHeritage{ClassBody}

ClassHeritage: ( Modified ) ClassExtendsClauseoptImplementsClauseopt

ClassExtendsClause: extends ClassType

ClassType: TypeReference

ImplementsClause: implementsClassOrInterfaceTypeList

ClassElement: ( Modified ) ConstructorDeclaration PropertyMemberDeclaration IndexMemberDeclaration

ConstructorDeclaration: AccessibilityModifieroptconstructor(ParameterListopt){FunctionBody} AccessibilityModifieroptconstructor(ParameterListopt);

PropertyMemberDeclaration: MemberVariableDeclaration MemberFunctionDeclaration MemberAccessorDeclaration

MemberVariableDeclaration: AccessibilityModifieroptstaticoptPropertyNameTypeAnnotationoptInitializeropt;

MemberFunctionDeclaration: AccessibilityModifieroptstaticoptPropertyNameCallSignature{FunctionBody} AccessibilityModifieroptstaticoptPropertyNameCallSignature;

MemberAccessorDeclaration: AccessibilityModifieroptstaticoptGetAccessor AccessibilityModifieroptstaticoptSetAccessor

IndexMemberDeclaration: IndexSignature;

Enums

EnumDeclaration: constoptenumBindingIdentifier{EnumBodyopt}

EnumBody: EnumMemberList,opt

EnumMemberList: EnumMember EnumMemberList,EnumMember

EnumMember: PropertyName PropertyName=EnumValue

EnumValue: AssignmentExpression

Namespaces

NamespaceDeclaration: namespaceIdentifierPath{NamespaceBody}

IdentifierPath: BindingIdentifier IdentifierPath.BindingIdentifier

NamespaceBody: NamespaceElementsopt

NamespaceElements: NamespaceElement NamespaceElementsNamespaceElement

NamespaceElement: Statement LexicalDeclaration FunctionDeclaration GeneratorDeclaration ClassDeclaration InterfaceDeclaration TypeAliasDeclaration EnumDeclaration NamespaceDeclaration AmbientDeclaration ImportAliasDeclaration ExportNamespaceElement

ExportNamespaceElement: exportVariableStatement exportLexicalDeclaration exportFunctionDeclaration exportGeneratorDeclaration exportClassDeclaration exportInterfaceDeclaration exportTypeAliasDeclaration exportEnumDeclaration exportNamespaceDeclaration exportAmbientDeclaration exportImportAliasDeclaration

ImportAliasDeclaration: importBindingIdentifier=EntityName;

EntityName: NamespaceName NamespaceName.IdentifierReference

Scripts and Modules

SourceFile: ImplementationSourceFile DeclarationSourceFile

ImplementationSourceFile: ImplementationScript ImplementationModule

DeclarationSourceFile: DeclarationScript DeclarationModule

ImplementationScript: ImplementationScriptElementsopt

ImplementationScriptElements: ImplementationScriptElement ImplementationScriptElementsImplementationScriptElement

ImplementationScriptElement: ImplementationElement AmbientModuleDeclaration

ImplementationElement: Statement LexicalDeclaration FunctionDeclaration GeneratorDeclaration ClassDeclaration InterfaceDeclaration TypeAliasDeclaration EnumDeclaration NamespaceDeclaration AmbientDeclaration ImportAliasDeclaration

DeclarationScript: DeclarationScriptElementsopt

DeclarationScriptElements: DeclarationScriptElement DeclarationScriptElementsDeclarationScriptElement

DeclarationScriptElement: DeclarationElement AmbientModuleDeclaration

DeclarationElement: InterfaceDeclaration TypeAliasDeclaration NamespaceDeclaration AmbientDeclaration ImportAliasDeclaration

ImplementationModule: ImplementationModuleElementsopt

ImplementationModuleElements: ImplementationModuleElement ImplementationModuleElementsImplementationModuleElement

ImplementationModuleElement: ImplementationElement ImportDeclaration ImportAliasDeclaration ImportRequireDeclaration ExportImplementationElement ExportDefaultImplementationElement ExportListDeclaration ExportAssignment

DeclarationModule: DeclarationModuleElementsopt

DeclarationModuleElements: DeclarationModuleElement DeclarationModuleElementsDeclarationModuleElement

DeclarationModuleElement: DeclarationElement ImportDeclaration ImportAliasDeclaration ExportDeclarationElement ExportDefaultDeclarationElement ExportListDeclaration ExportAssignment

ImportRequireDeclaration: importBindingIdentifier=require(StringLiteral);

ExportImplementationElement: exportVariableStatement exportLexicalDeclaration exportFunctionDeclaration exportGeneratorDeclaration exportClassDeclaration exportInterfaceDeclaration exportTypeAliasDeclaration exportEnumDeclaration exportNamespaceDeclaration exportAmbientDeclaration exportImportAliasDeclaration

ExportDeclarationElement: exportInterfaceDeclaration exportTypeAliasDeclaration exportAmbientDeclaration exportImportAliasDeclaration

ExportDefaultImplementationElement: exportdefaultFunctionDeclaration exportdefaultGeneratorDeclaration exportdefaultClassDeclaration exportdefaultAssignmentExpression;

ExportDefaultDeclarationElement: exportdefaultAmbientFunctionDeclaration exportdefaultAmbientClassDeclaration exportdefaultIdentifierReference;

ExportListDeclaration: export*FromClause; exportExportClauseFromClause; exportExportClause;

ExportAssignment: export=IdentifierReference;

Ambients

AmbientDeclaration: declareAmbientVariableDeclaration declareAmbientFunctionDeclaration declareAmbientClassDeclaration declareAmbientEnumDeclaration declareAmbientNamespaceDeclaration

AmbientVariableDeclaration: varAmbientBindingList; letAmbientBindingList; constAmbientBindingList;

AmbientBindingList: AmbientBinding AmbientBindingList,AmbientBinding

AmbientBinding: BindingIdentifierTypeAnnotationopt

AmbientFunctionDeclaration: functionBindingIdentifierCallSignature;

AmbientClassDeclaration: classBindingIdentifierTypeParametersoptClassHeritage{AmbientClassBody}

AmbientClassBody: AmbientClassBodyElementsopt

AmbientClassBodyElements: AmbientClassBodyElement AmbientClassBodyElementsAmbientClassBodyElement

AmbientClassBodyElement: AmbientConstructorDeclaration AmbientPropertyMemberDeclaration IndexSignature

AmbientConstructorDeclaration: constructor(ParameterListopt);

AmbientPropertyMemberDeclaration: AccessibilityModifieroptstaticoptPropertyNameTypeAnnotationopt; AccessibilityModifieroptstaticoptPropertyNameCallSignature;

AmbientEnumDeclaration: EnumDeclaration

AmbientNamespaceDeclaration: namespaceIdentifierPath{AmbientNamespaceBody}

AmbientNamespaceBody: AmbientNamespaceElementsopt

AmbientNamespaceElements: AmbientNamespaceElement AmbientNamespaceElementsAmbientNamespaceElement

AmbientNamespaceElement: exportoptAmbientVariableDeclaration exportoptAmbientLexicalDeclaration exportoptAmbientFunctionDeclaration exportoptAmbientClassDeclaration exportoptInterfaceDeclaration exportoptAmbientEnumDeclaration exportoptAmbientNamespaceDeclaration exportoptImportAliasDeclaration

AmbientModuleDeclaration: declaremoduleStringLiteral{ DeclarationModule}