[英]Is it OK to cast undefined to void in TS?
function isUndefined (payload: any): payload is undefined | void {
return payload === undefined
}
In TypeScript, I have a function that can return either something or undefined
or void
.在 TypeScript 中,我有一个函数可以返回 something 或undefined
或void
。
Something like an event handler that can return a modified payload or the dev can choose to return nothing or undefined in case they won't modify the payload:像一个事件处理程序,可以返回修改后的有效负载,或者开发人员可以选择不返回任何内容或未定义,以防他们不修改有效负载:
function eventHandler <T extends {[key: string]: any}> (payload: T): Modified<T> | undefined | void {
// ... implementation
}
Then I have a type checker that needs to check if it's returning something other than void or undefined:然后我有一个类型检查器,需要检查它是否返回了 void 或 undefined 以外的内容:
const result = eventHandler(payload)
if (result !== undefined) {
// we have a modified payload!
}
However, is the above snippet I get an error because it says that even though result !== undefined
it can still be void
?但是,上面的代码片段是不是我得到了一个错误,因为它说即使result !== undefined
它仍然可以是void
?
In my opinion, I think it's peculiar because void
should be the same as undefined
.在我看来,我认为它很奇怪,因为void
应该与undefined
相同。
So I made this type checker that solves it:所以我做了这个类型检查器来解决它:
function isUndefined (payload: any): payload is undefined | void {
return payload === undefined
}
This solves my issue, but my question is:这解决了我的问题,但我的问题是:
Is this OK?这个可以吗? Or is it bad practice?或者这是不好的做法?
void
is not undefined
. void
不是undefined
。 void
means the absence of a return value. void
表示没有返回值。 undefined
is the type of the value undefined at runtime. undefined
是运行时 undefined 值的类型。
it is true that a function that returns no value at run time returns undefined
, but in the TS type system we chose to make the absence of a return value special.在运行时不返回任何值的函数确实会返回undefined
,但在 TS 类型系统中,我们选择使没有返回值的情况变得特殊。
For example assigning (a) => void to (a) => number | undefined
例如将(a) => void to (a) => number | undefined
分配(a) => void to (a) => number | undefined
(a) => void to (a) => number | undefined
is likely an error, though it is safe at run-time. (a) => void to (a) => number | undefined
可能是一个错误,尽管它在运行时是安全的。
In general do not use void except in the return type of functions.除了函数的返回类型外,一般不要使用 void。 for everything else, use undefined
.对于其他所有内容,请使用undefined
。
So, yes we will need to use different check for undefined
and void
.所以,是的,我们需要对undefined
和void
使用不同的检查。
I think you're making this more complicated that it needs to be.我认为你让这变得更复杂了。 A function that returns void
can:返回void
的函数可以:
return;
有return;
statement with no value specified.没有指定值的语句。return undefined;
有一个return undefined;
的return undefined;
statement.陈述。 In pure javascript, all of the above would have a return value of undefined
.在纯 javascript 中,上述所有内容的返回值都是undefined
。 If you say a function returns undefined
, then you can only do #2 and #3 from the above list.如果你说一个函数返回undefined
,那么你只能从上面的列表中做 #2 和 #3。
So you can just have a function type that unions void
with whatever something you want.所以,你可以只是有工会的功能型void
与任何你想要的东西。
function foo(): string | void {
return Math.random() > 0.5 ? 'abc' : 123
}
const val = foo()
if (val === undefined) {
console.log('is undefined', val)
} else {
console.log('is some value', val)
}
This means that you could create a generic function type that modifies payloads like so:这意味着您可以创建一个通用函数类型来修改有效负载,如下所示:
type PayloadModifier<T extends {[key: string]: any}> = (payload: T) => T | void
const setMaxAsTen: PayloadModifier<{a: number}> = (payload) => {
if (payload.a > 10) {
return { a: 10 }
}
return undefined // required unless noImplicitReturns is false
}
const val = setMaxAsTen({a: 5})
if (val === undefined) {
console.log('is undefined', val)
} else {
console.log('is some value', val)
}
Last thing to note is that there is a compiler option that is good to leave on called noImplicitReturns
.最后要注意的是,有一个编译器选项可以很好地保留, 称为noImplicitReturns
。 if a function declares a return value in any execution branch, then a return value must be declared in every execution branch.如果函数在任何执行分支中声明了返回值,则必须在每个执行分支中声明返回值。 So because the above sometimes returns a value, you must explicitly return undefined
if you don't to return a payload.所以因为上面有时会返回一个值,如果你不返回一个有效负载,你必须明确地返回undefined
。 You can toggle off that option, allowing you to omit that line, but it's not recommended as it does help you catch some bugs.您可以关闭该选项,允许您省略该行,但不建议这样做,因为它可以帮助您捕获一些错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.