[TypeScript] 编程实践之1: Google的TypeScript代码风格13:语法

A 语法

本附录包含在主文档中找到的语法摘要。 如2.1节所述,TypeScript语法是ECMAScript 2015语言规范(特别是ECMA-262标准,第6版)中定义的语法的超集,本附录仅列出从ECMAScript语法中新增或修改的产品。

A.1 类型

TypeParameters:
	< TypeParameterList >

TypeParameterList:
	TypeParameter
	TypeParameterList , TypeParameter

TypeParameter:
	BindingIdentifier Constraintopt

Constraint:
	extends Type

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:
	new TypeParametersopt ( ParameterListopt ) => Type

TypeQuery:
	typeof TypeQueryExpression

TypeQueryExpression:
	IdentifierReference
	TypeQueryExpression . IdentifierName

ThisType:
	this

PropertySignature:
	PropertyName ?opt TypeAnnotationopt

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:
	AccessibilityModifieropt BindingIdentifierOrPattern TypeAnnotationopt
	BindingIdentifier : StringLiteral

AccessibilityModifier:
	public
	private
	protected

BindingIdentifierOrPattern:
	BindingIdentifier
	BindingPattern

OptionalParameterList:
	OptionalParameter
	OptionalParameterList , OptionalParameter

OptionalParameter:
	AccessibilityModifieropt BindingIdentifierOrPattern ? TypeAnnotationopt
	AccessibilityModifieropt BindingIdentifierOrPattern TypeAnnotationopt Initializer
	BindingIdentifier ? : StringLiteral

RestParameter:
	... BindingIdentifier TypeAnnotationopt

ConstructSignature:
	new TypeParametersopt ( ParameterListopt ) TypeAnnotationopt

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

MethodSignature:
	PropertyName ?opt CallSignature

TypeAliasDeclaration:
	type BindingIdentifier TypeParametersopt = Type ;

A.2 表达式

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

GetAccessor:
	get PropertyName ( ) TypeAnnotationopt { FunctionBody }

SetAccessor:
	set PropertyName ( BindingIdentifierOrPattern TypeAnnotationopt ) { FunctionBody }

FunctionExpression: ( Modified )
	function BindingIdentifieropt CallSignature { FunctionBody }

ArrowFormalParameters: ( Modified )
	CallSignature

Arguments: ( Modified )
	TypeArgumentsopt ( ArgumentListopt )

UnaryExpression: ( Modified )
	…
	< Type > UnaryExpression

A.3 语句

Declaration: ( Modified )
	…
	InterfaceDeclaration
	TypeAliasDeclaration
	EnumDeclaration

VariableDeclaration: ( Modified )
	SimpleVariableDeclaration
	DestructuringVariableDeclaration

SimpleVariableDeclaration:
	BindingIdentifier TypeAnnotationopt Initializeropt

DestructuringVariableDeclaration:
	BindingPattern TypeAnnotationopt Initializer

LexicalBinding: ( Modified )
	SimpleLexicalBinding
	DestructuringLexicalBinding

SimpleLexicalBinding:
	BindingIdentifier TypeAnnotationopt Initializeropt

DestructuringLexicalBinding:
	BindingPattern TypeAnnotationopt Initializeropt

A.4 函数

FunctionDeclaration: ( Modified )
	function BindingIdentifieropt CallSignature { FunctionBody }
	function BindingIdentifieropt CallSignature ;

A.5 接口

InterfaceDeclaration:
	interface BindingIdentifier TypeParametersopt InterfaceExtendsClauseopt ObjectType

InterfaceExtendsClause:
	extends ClassOrInterfaceTypeList

ClassOrInterfaceTypeList:
	ClassOrInterfaceType
	ClassOrInterfaceTypeList , ClassOrInterfaceType

ClassOrInterfaceType:
	TypeReference

A.6 类

ClassDeclaration: ( Modified )
	class BindingIdentifieropt TypeParametersopt ClassHeritage { ClassBody }

ClassHeritage: ( Modified )
	ClassExtendsClauseopt ImplementsClauseopt

ClassExtendsClause:
	extends  ClassType

ClassType:
	TypeReference

ImplementsClause:
	implements ClassOrInterfaceTypeList

ClassElement: ( Modified )
	ConstructorDeclaration
	PropertyMemberDeclaration
	IndexMemberDeclaration

ConstructorDeclaration:
	AccessibilityModifieropt constructor ( ParameterListopt ) { FunctionBody }
	AccessibilityModifieropt constructor ( ParameterListopt ) ;

PropertyMemberDeclaration:
	MemberVariableDeclaration
	MemberFunctionDeclaration
	MemberAccessorDeclaration

MemberVariableDeclaration:
	AccessibilityModifieropt staticopt PropertyName TypeAnnotationopt Initializeropt ;

MemberFunctionDeclaration:
	AccessibilityModifieropt staticopt PropertyName CallSignature { FunctionBody }
	AccessibilityModifieropt staticopt PropertyName CallSignature ;

MemberAccessorDeclaration:
	AccessibilityModifieropt staticopt GetAccessor
	AccessibilityModifieropt staticopt SetAccessor

IndexMemberDeclaration:
	IndexSignature ;

A.7 枚举

EnumDeclaration:
	constopt enum BindingIdentifier { EnumBodyopt }

EnumBody:
	EnumMemberList ,opt

EnumMemberList:
	EnumMember
	EnumMemberList , EnumMember

EnumMember:
	PropertyName
	PropertyName = EnumValue

EnumValue:
	AssignmentExpression

A.8 命名空间

NamespaceDeclaration:
	namespace IdentifierPath { NamespaceBody }

IdentifierPath:
	BindingIdentifier
	IdentifierPath . BindingIdentifier

NamespaceBody:
	NamespaceElementsopt

NamespaceElements:
	NamespaceElement
	NamespaceElements NamespaceElement

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

ExportNamespaceElement:
	export VariableStatement
	export LexicalDeclaration
	export FunctionDeclaration
	export GeneratorDeclaration
	export ClassDeclaration
	export InterfaceDeclaration
	export TypeAliasDeclaration
	export EnumDeclaration
	export NamespaceDeclaration
	export AmbientDeclaration
	export ImportAliasDeclaration

ImportAliasDeclaration:
	import BindingIdentifier = EntityName ;

EntityName:
	NamespaceName
	NamespaceName . IdentifierReference

A.9 Script和Modules

SourceFile:
	ImplementationSourceFile
	DeclarationSourceFile

ImplementationSourceFile:
	ImplementationScript
	ImplementationModule

DeclarationSourceFile:
	DeclarationScript
	DeclarationModule

ImplementationScript:
	ImplementationScriptElementsopt

ImplementationScriptElements:
	ImplementationScriptElement
	ImplementationScriptElements ImplementationScriptElement

ImplementationScriptElement:
	ImplementationElement
	AmbientModuleDeclaration

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

DeclarationScript:
	DeclarationScriptElementsopt

DeclarationScriptElements:
	DeclarationScriptElement
	DeclarationScriptElements DeclarationScriptElement

DeclarationScriptElement:
	DeclarationElement
	AmbientModuleDeclaration

DeclarationElement:
	InterfaceDeclaration
	TypeAliasDeclaration
	NamespaceDeclaration
	AmbientDeclaration
	ImportAliasDeclaration

ImplementationModule:
	ImplementationModuleElementsopt

ImplementationModuleElements:
	ImplementationModuleElement
	ImplementationModuleElements ImplementationModuleElement

ImplementationModuleElement:
	ImplementationElement
	ImportDeclaration
	ImportAliasDeclaration
	ImportRequireDeclaration
	ExportImplementationElement
	ExportDefaultImplementationElement
	ExportListDeclaration
	ExportAssignment

DeclarationModule:
	DeclarationModuleElementsopt

DeclarationModuleElements:
	DeclarationModuleElement
	DeclarationModuleElements DeclarationModuleElement

DeclarationModuleElement:
	DeclarationElement
	ImportDeclaration
	ImportAliasDeclaration
	ExportDeclarationElement
	ExportDefaultDeclarationElement
	ExportListDeclaration
	ExportAssignment

ImportRequireDeclaration:
	import BindingIdentifier = require ( StringLiteral ) ;

ExportImplementationElement:
	export VariableStatement
	export LexicalDeclaration
	export FunctionDeclaration
	export GeneratorDeclaration
	export ClassDeclaration
	export InterfaceDeclaration
	export TypeAliasDeclaration
	export EnumDeclaration
	export NamespaceDeclaration
	export AmbientDeclaration
	export ImportAliasDeclaration

ExportDeclarationElement:
	export InterfaceDeclaration
	export TypeAliasDeclaration
	export AmbientDeclaration
	export ImportAliasDeclaration

ExportDefaultImplementationElement:
	export default FunctionDeclaration
	export default GeneratorDeclaration
	export default ClassDeclaration
	export default AssignmentExpression ;

ExportDefaultDeclarationElement:
	export default AmbientFunctionDeclaration
	export default AmbientClassDeclaration
	export default IdentifierReference ;

ExportListDeclaration:
	export * FromClause ;
	export ExportClause FromClause ;
	export ExportClause ;

ExportAssignment:
	export = IdentifierReference ;

A.10 Ambient

AmbientDeclaration:
	declare AmbientVariableDeclaration
	declare AmbientFunctionDeclaration
	declare AmbientClassDeclaration
	declare AmbientEnumDeclaration
	declare AmbientNamespaceDeclaration

AmbientVariableDeclaration:
	var AmbientBindingList ;
	let AmbientBindingList ;
	const AmbientBindingList ;

AmbientBindingList:
	AmbientBinding
	AmbientBindingList , AmbientBinding

AmbientBinding:
	BindingIdentifier TypeAnnotationopt

AmbientFunctionDeclaration:
	function BindingIdentifier CallSignature ;

AmbientClassDeclaration:
	class BindingIdentifier TypeParametersopt ClassHeritage { AmbientClassBody }

AmbientClassBody:
	AmbientClassBodyElementsopt

AmbientClassBodyElements:
	AmbientClassBodyElement
	AmbientClassBodyElements AmbientClassBodyElement

AmbientClassBodyElement:
	AmbientConstructorDeclaration
	AmbientPropertyMemberDeclaration
	IndexSignature

AmbientConstructorDeclaration:
	constructor ( ParameterListopt ) ;

AmbientPropertyMemberDeclaration:
	AccessibilityModifieropt staticopt PropertyName TypeAnnotationopt ;
	AccessibilityModifieropt staticopt PropertyName CallSignature ;

AmbientEnumDeclaration:
	EnumDeclaration

AmbientNamespaceDeclaration:
	namespace IdentifierPath { AmbientNamespaceBody }

AmbientNamespaceBody:
	AmbientNamespaceElementsopt

AmbientNamespaceElements:
	AmbientNamespaceElement
	AmbientNamespaceElements AmbientNamespaceElement

AmbientNamespaceElement:
	exportopt AmbientVariableDeclaration
	exportopt AmbientLexicalDeclaration
	exportopt AmbientFunctionDeclaration
	exportopt AmbientClassDeclaration
	exportopt InterfaceDeclaration
	exportopt AmbientEnumDeclaration
	exportopt AmbientNamespaceDeclaration
	exportopt ImportAliasDeclaration

AmbientModuleDeclaration:
	declare module StringLiteral {  DeclarationModule }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值