简体   繁体   English

打字稿功能怪异的空隙|| &&行为

[英]Typescript Function Weird Void || && Behaviour

Why is returnObject causing an compilation TypeError returnObject2 isn't? 为什么returnObject导致编译TypeError returnObject2不是?

Normally, void || something 通常, void || something void || something should return something while void && something should return void . void || something应该返回somethingvoid && something应该返回void

But in Typescript, it's the opposite that occurs. 但是在Typescript中,情况恰恰相反。

var returnVoid = function(){};

var returnObject = function(){  //Typescript compilation TypeError. 
    //No best common type exists among return expressions.
    if(Math.random() < 0.5)
        return new Error();
    return returnVoid() || null; //always return null
}
var returnObject2 = function(){ //works
    if(Math.random() < 0.5)
        return new Error();
    return returnVoid() && null; //always return undefined
}

Note: TypeError occurs during the compilation, not in the runtime. 注意:TypeError发生在编译期间,而不是在运行时。


EDIT: I did another test. 编辑:我做了另一个测试。 Shouldn't returnNum2 be () => number too considering (undefined || something) === something ? 不宜returnNum2() => number过多考虑(undefined || something) === something Note: Same behaviour for void 0 . 注意: void 0行为相同。

var returnNum = function(){ //() => number
    return undefined || 0; 
}

var returnVoid = function(){};

var returnNum2 = function(){ //() => void | number
    return returnVoid() || 0; 
}

It's been pointed out in comments, but I think in general you just need to understand that function(){} will return undefined , which has specified behavior for logical operators. 注释中已经指出了这一点,但是我认为通常您只需要了解function(){}将返回undefined ,该行为为逻辑运算符指定了行为。

For undefined && somevalue , undefined will always be returned. 对于undefined && somevalue ,将始终返回undefined。 For undefined || somevalue 对于undefined || somevalue undefined || somevalue , somevalue will be evaluated and returned. undefined || somevalue ,somevalue将被评估并返回。

Here's a good reference for more information: http://www.javascriptkit.com/jsref/comparison_operators.shtml 这是获得更多信息的很好的参考: http : //www.javascriptkit.com/jsref/comparison_operators.shtml

EDIT: The question isn't about what is returned for the logical operation, but why typescript gives error TS2354: No best common type exists among return expressions. 编辑:问题不是关于逻辑操作返回什么,而是为什么打字稿给出error TS2354: No best common type exists among return expressions. on compilation. 在编译。

This does seem like an error, but may make sense in the context. 这似乎确实是一个错误,但在上下文中可能是有道理的。 If you replace the logical operators with just a call to returnVoid() only, you'll get the same error in both functions. 如果仅使用对returnVoid()的调用来替换逻辑运算符,则在两个函数中都会得到相同的错误。 Static typing allows the && operator to short-circuit entirely for typing, since something && null will never evaluate to a type, but something || null 静态类型允许&&运算符完全短路以进行类型输入,因为something && null永远不会求值为类型,而something || null something || null could depending on what the something is. something || null取决于什么是something

Related to this, in typescript you cannot explicitly specify null or undefined as a return type for a function. 与此相关,在typescript中,您不能显式指定null或undefined作为函数的返回类型。

While I understand why this may be the case, I agree it is a little odd. 虽然我知道为什么会这样,但我同意这有点奇怪。 It might be worth checking with the folks who make Typescript and filing a bug. 与制作Typescript并提交错误的人员进行检查可能值得一试。

TypeScript doesn't special case expressions of the form undefined || T TypeScript不使用undefined || T形式的特殊情况表达式 undefined || T or void || T undefined || Tvoid || T void || T to be T because a) you shouldn't write that code (use the comma operator!) and b) it's not safe to write this code because return value contravariance means you're not guaranteed to have a falsy value just because you have a void-returning function reference. void || T变为T是因为a)您不应该编写该代码(使用逗号运算符!),并且b)编写此代码并不安全,因为返回值的矛盾意味着您不能仅仅因为拥有无效返回函数参考。

Consider if you wrote code like this: 考虑是否编写了这样的代码:

type callback = (arg: any) => void;

function doSomething(x: callback) {
    return x(10) || 'Hello, world!';
}

var x = [];
var add = (arg: any) => x.push(arg);
console.log(doSomething(add)); // Prints '1', not 'Hello, world!'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM