我只是在寻找一个原因为什么这是无效的:
() => throw 42;
我知道我可以通过以下方式绕过它:
() => {throw 42};
你不能
return throw
这实际上就是你想要做的:
function(){
return throw 42;
}
要破解,您可以使用函数包装
throw
。然后将其用作表达式的一部分。
function explode (str?: string) { throw new Error(str) };
const oups = () => explode("42");
oups();
function explode (str?: string) { throw new Error(str) };
const input: null | number = null;
const output = (input ?? explode("input should not be null")) + 42;