typescript 如何检查是否提供了可选参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20940545/
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
How can I check whether an optional parameter was provided?
提问by Sam
Given a function with optional parameters:
给定一个带有可选参数的函数:
function DoSomething(a, b?) {
/** Implementation */
}
How can I determine whether an optional parameter was provided from within the function body? Currently, the best way to do this that I can think of is:
如何确定是否从函数体内提供了可选参数?目前,我能想到的最好的方法是:
typeof b === 'undefined'
But this is kind of messy and not straight-forward to read. Since TypeScript provides optional parameter support, I'm hoping it also has an intuitive way to check if a parameter was provided.
但这有点凌乱,读起来不直截了当。由于 TypeScript 提供了可选参数支持,我希望它也有一种直观的方式来检查是否提供了参数。
As the above example shows, I don't mind whether the optional parameter was explicitly set to undefined
or not supplied at all.
如上面的示例所示,我不介意可选参数是否显式设置为undefined
或根本不提供。
Edit
编辑
Unfortunately, this question wasn't as clear as it should have been, particularly if it's skim-read. It was meant to be about how to cleanlycheck if an optional parameter is completely omitted, as in:
不幸的是,这个问题并不像它应该的那样清楚,特别是如果它是脱脂阅读。它的目的是关于如何干净地检查是否完全省略了可选参数,如下所示:
DoSomething("some value");
DoSomething("some value");
I've accepted Evan's answer since his solution (b === undefined
) is cleaner than the one in my question (typeof b === 'undefined'
) while still having the same behaviour.
我已经接受了 Evan 的回答,因为他的解决方案 ( b === undefined
) 比我的问题 ( typeof b === 'undefined'
) 中的解决方案更简洁,同时仍然具有相同的行为。
The other answers are definitely useful, and which answer is correct for you depends on your use case.
其他答案绝对有用,哪个答案适合您取决于您的用例。
采纳答案by Evan Trimboli
You can just check the value to see if it's undefined:
您可以检查该值以查看它是否未定义:
var fn = function(a, b) {
console.log(a, b === undefined);
};
fn(1);
fn(2, undefined);
fn(3, null);
回答by Tod Birdsall
After googling "typescript check for undefined", I saw this question at the top of the results, but the answer given by Evan Trimboli did not solve my problem.
在谷歌搜索“typescript check for undefined”之后,我在结果的顶部看到了这个问题,但是 Evan Trimboli 给出的答案并没有解决我的问题。
Here is the answerthat ultimately solved my problem. The following code is what I settled on. It should work in cases where the value equals null
or undefined
:
这是最终解决我问题的答案。以下代码是我确定的。它应该在值等于null
或的情况下工作undefined
:
function DoSomething(a, b?) {
if (b == null) doSomething();
}
回答by Vladimir Prudnikov
TypeScript's util
module has function isUndefined()
. You can use it like this.
TypeScript 的util
模块具有 function isUndefined()
。你可以像这样使用它。
import {isUndefined} from "util";
class A {
test(b?: string): string {
if (isUndefined(b)) {
return "UNDEFINED";
} else {
return ("DEFINED" + b);
}
}
}
回答by Tobias Koller
you could simple add an optional parameter with an default value like this
您可以简单地添加一个具有默认值的可选参数,如下所示
function DoSomething(a, b: boolean=null) {
if(b == null)
{
//parameter was not set
}
else
{
//parameter is true or false...no other option available
}
}