Stage 0 Draft / September 6, 2023

Types as Comments Tentative Grammar

1 Organization and Notational Conventions

Currently this grammar is split into modified productions of ECMA-262 and new productions that are specific to this proposal.

This tentative grammar takes some notational liberties beyond the conventions in ECMA-262. This is a temporary accommodation for authors in order to avoid extensive duplication of ECMA-262's grammar. Within Modified Productions, RHS productions with insert-formatted text (such as this text) indicates the addition of these RHS productions to existing productions in ECMA-262. Otherwise, RHS productions in Modified Productions are intended to replace the original productions in ECMA-262.

2 Modified Productions



MemberExpression : MemberExpression :: TypeArguments MemberExpression [no LineTerminator here] ! CallExpression : CallExpression :: TypeArguments OptionalChain : OptionalChain :: TypeArguments RelationalExpression : RelationalExpression [no LineTerminator here] as Type RelationalExpression [no LineTerminator here] as const Declaration : TypeDeclaration InterfaceDeclaration LexicalBinding : BindingIdentifier TypeAnnotationopt Initializeropt BindingPattern TypeAnnotationopt Initializer VariableDeclaration : BindingIdentifier TypeAnnotationopt Initializeropt BindingPattern TypeAnnotationopt Initializer Catch : catch ( CatchParameter TypeAnnotationopt ) Block catch Block FormalParameter : BindingIdentifier TypeAnnotationopt Initializeropt BindingIdentifier ? TypeAnnotationopt BindingPattern TypeAnnotationopt Initializeropt FunctionRestParameter : ... BindingIdentifier TypeAnnotationopt ... BindingPattern TypeAnnotationopt FunctionDeclaration : function BindingIdentifier TypeParametersopt ( FormalParameters ) TypeAnnotationopt { FunctionBody } FunctionExpression : function BindingIdentifieropt TypeParametersopt ( FormalParameters ) TypeAnnotationopt { FunctionBody } ArrowFunction : ArrowParameters TypeAnnotation => ConciseBody ArrowParameters : TypeParameters ( UniqueFormalParameters ) MethodDefinition : ClassElementName ?opt TypeParametersopt ( UniqueFormalParameters ) TypeAnnotationopt { FunctionBody } GeneratorMethod AsyncMethod AsyncGeneratorMethod get ClassElementName ( ) TypeAnnotationopt { FunctionBody } set ClassElementName ( PropertySetParameterList ) { FunctionBody } GeneratorDeclaration : function * BindingIdentifier TypeParametersopt ( FormalParameters ) TypeAnnotationopt { GeneratorBody } GeneratorExpression : function * BindingIdentifieropt TypeParametersopt ( FormalParameters ) TypeAnnotationopt { GeneratorBody } GeneratorMethod : * ClassElementName ?opt TypeParametersopt ( UniqueFormalParameters ) TypeAnnotationopt { GeneratorBody } AsyncGeneratorDeclaration : async [no LineTerminator here] function * BindingIdentifier TypeParametersopt ( FormalParameters ) TypeAnnotationopt { AsyncGeneratorBody } AsyncGeneratorExpression : async [no LineTerminator here] function * BindingIdentifieropt TypeParametersopt ( FormalParameters ) TypeAnnotationopt { AsyncGeneratorBody } AsyncGeneratorMethod : async [no LineTerminator here] * ClassElementName ?opt TypeParametersopt ( UniqueFormalParameters ) TypeAnnotationopt { AsyncGeneratorBody } ClassDeclaration : AbstractModifieropt class BindingIdentifier ClassTail ClassExpression : AbstractModifieropt class BindingIdentifieropt ClassTail ClassHeritage : extends LeftHandSideExpression TypeArgumentsopt ClassImplementsClauseopt FieldDefinition : ClassElementName ?opt TypeAnnotationopt Initializeropt ClassElementName ! TypeAnnotationopt Initializeropt ClassElement : AccessibilityModifieropt OverrideModifieropt MethodDefinition AccessibilityModifieropt static OverrideModifieropt MethodDefinition AccessibilityModifieropt FieldDefinition AccessibilityModifieropt static FieldDefinition ClassStaticBlock AbstractClassElement IndexSignature ; AsyncFunctionDeclaration : async [no LineTerminator here] function BindingIdentifier TypeParametersopt ( FormalParameters ) TypeAnnotationopt { AsyncFunctionBody } AsyncFunctionExpression : async [no LineTerminator here] function BindingIdentifieropt TypeParametersopt ( FormalParameters ) TypeAnnotationopt { AsyncFunctionBody } AsyncMethod : async [no LineTerminator here] ClassElementName ?opt TypeParametersopt ( UniqueFormalParameters ) TypeAnnotationopt { AsyncFunctionBody } CoverCallExpressionAndAsyncArrowHead : MemberExpression TypeParametersopt Arguments TypeAnnotationopt AsyncArrowHead : async [no LineTerminator here] TypeParametersopt ArrowFormalParameters ImportDeclaration : import type ImportClause FromClause ; ImportSpecifier : type ImportedBinding type ModuleExportName as ImportedBinding

3 New Productions


TypeArguments : AngleBracketedTokens TypeDeclaration : type BindingIdentifier TypeParametersopt = Type TypeParameters : AngleBracketedTokens Type : ConditionalType NonConditionalType ConditionalType : NonConditionalType [no LineTerminator here] extends NonConditionalType ? Type : Type NonConditionalType : UnionType FunctionType ConstructorType UnionType : |opt IntersectionType UnionType | IntersectionType IntersectionType : &opt TypeOperatorType IntersectionType & TypeOperatorType TypeOperatorType : readonly TypeOperatorType keyof TypeOperatorType unique TypeOperatorType infer TypeOperatorType not TypeOperatorType PrimaryType PrimaryType : ParenthesizedType SquareBracketedType CurlyBracketedType TypeReference ArrayType LiteralType TypeQuery ImportType TypePredicate this void ParenthesizedType : ParenthesizedTokens SquareBracketedType : SquareBracketedTokens CurlyBracketedType : CurlyBracketedTokens TypeReference : TypeName [no LineTerminator here] TypeArgumentsopt TypeName : Identifier TypeName . Identifier ArrayType : PrimaryType [no LineTerminator here] [ ] LiteralType : NumericLiteralType StringLiteral TemplateLiteralType true false null TemplateLiteralType : NoSubstitutionTemplate TemplateBracketedTokens NumericLiteralType : NumericLiteral - [no LineTerminator here] NumericLiteral TypeQuery : typeof [no LineTerminator here] EntityName EntityName : IdentifierName ImportSpecifier EntityName . IdentifierName EntityName :: TypeArguments ImportSpecifier : import [no LineTerminator here] ( ModuleSpecifier ) ImportType : ImportSpecifier ImportSpecifier . TypeName TypePredicate : IdentifierOrThis [no LineTerminator here] is Type asserts IdentifierOrThis asserts IdentifierOrThis [no LineTerminator here] is Type IdentifierOrThis : Identifier this FunctionType : TypeParametersopt ParameterList => Type ConstructorType : new TypeParametersopt ParameterList => Type ParameterList : ParenthesizedTokens InterfaceDeclaration : interface BindingIdentifier TypeParametersopt InterfaceExtendsClauseopt InterfaceBody InterfaceExtendsClause : extends ClassOrInterfaceTypeList ClassOrInterfaceTypeList : TypeReference ClassOrInterfaceTypeList , TypeReference InterfaceBody : CurlyBracketedTokens TypeAnnotation : : Type AbstractModifier : abstract ClassImplementsClause : implements ClassOrInterfaceTypeList AccessibilityModifier : public protected private OverrideModifier : override AbstractClassElement : AccessibilityModifieropt abstract OverrideModifieropt AbstractMethodDefinition AccessibilityModifieropt abstract AbstractFieldDefinition AbstractMethodDefinition : ClassElementName TypeParametersopt ( UniqueFormalParameters ) TypeAnnotationopt get ClassElementName ( ) TypeAnnotationopt set ClassElementName ( PropertySetParameterList ) AbstractFieldDefinition : ClassElementName ?opt TypeAnnotationopt IndexSignature : [ BindingIdentifier TypeAnnotation ] TypeAnnotation BracketedTokens : ParenthesizedTokens SquareBracketedTokens CurlyBracketedTokens AngleBracketedTokens TemplateBracketedTokens ParenthesizedTokens : ( TokenBodyopt ) SquareBracketedTokens : [ TokenBodyopt ] CurlyBracketedTokens : { TokenBodyopt } AngleBracketedTokens : < TokenBodyopt > TemplateBracketedTokens : TemplateHead TemplateTokenBody TemplateTail TemplateTokenBody : TokenBody TokenBody TemplateMiddle TemplateTokenBody TokenBody : TokenOrBracketedTokens TokenBodyopt TokenOrBracketedTokens : NonBracketedToken BracketedTokens NonBracketedToken : Token but not one of ( or ) or [ or ] or { or } or < or > or TemplateHead or TemplateMiddle or TemplateTail