typescript 如何修复错误 TS1015:参数不能有问号和初始值设定项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17186566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:49:29  来源:igfitidea点击:

How do I fix error TS1015: Parameter cannot have question mark and initializer?

typescript

提问by

I have just upgraded to TypeScript beta 0.9 and now I'm getting the error:

我刚刚升级到 TypeScript beta 0.9,现在出现错误:

TS1015: Parameter cannot have question mark and initializer

TS1015:参数不能有问号和初始值设定项

This was valid before, how do I fix this?

这以前是有效的,我该如何解决这个问题?

Here's an example of the code that's generating this error:

这是生成此错误的代码示例:

functionName(parameterName?: typeName = defaultValue): typeName

回答by

If you look in the TypeScript Language Specificationdocument you can find a wealth of detail about the language syntax.

如果您查看TypeScript 语言规范文档,您可以找到有关语言语法的大量详细信息。

Section 3.9.2describes call signatures i.e. the syntax used to call functions and constructors etc.

3.9.2节描述了调用签名,即用于调用函数和构造函数等的语法。

Section 3.9.2.2is specifically about the parameters associated with a call.

3.9.2.2节专门介绍与调用相关的参数。

It defines optional parameters as:

它将可选参数定义为:

AccessibilityModifieropt?BindingIdentifierOrPattern???TypeAnnotationopt

AccessibilityModifieropt?BindingIdentifierOrPattern?TypeAnnotationopt?Initializer

AccessibilityModifieropt?BindingIdentifierOrPattern???TypeAnnotationopt

AccessibilityModifieropt?BindingIdentifierOrPattern?TypeAnnotationopt?Initializer

We can see see that either using '?' ORproviding a default value Initializerwill mark the parameter as being optional.

我们可以看到,要么使用'?' OR提供默认值Initializer会将参数标记为可选。

So to fix the compiler error you can simply remove the '?' and leave the default value and it will remain as an optional parameter as you intend.

因此,要修复编译器错误,您可以简单地删除“?” 并保留默认值,它将按照您的意愿保留为可选参数。