![](/img/trans.png)
[英]Destructured arguments under optional function arguments in typescript throw error
[英]TypeScript and optional destructured arguments
我似乎无法获得可选参数与TypeScript中的析构参数一起使用。
为参数生成了正确的代码,但Typescript似乎不允许我在代码中使用生成的变量,从而破坏了目的。
难道我做错了什么? 这是一个减少:
declare var lastDirectionWasDownish : boolean;
function goToNext(
{
root: Node = document.activeElement,
actLikeLastDirectionWasDownish:boolean = lastDirectionWasDownish
} = {}
) {
return root && actLikeLastDirectionWasDownish;
}
汇编成
function goToNext(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.root, Node = _c === void 0 ? document.activeElement : _c, _d = _b.actLikeLastDirectionWasDownish, boolean = _d === void 0 ? lastDirectionWasDownish : _d;
return root && actLikeLastDirectionWasDownish;
}
TypeScript实际上阻止你犯一个你在纯JS中会错过的错误。 以下纯JS:
function foo({root: Node}) {
// the parameter `root` has been copied to `Node`
}
TypeScript理解这一点,不允许您使用Node
。 要添加类型注释,您实际上是:
function foo({root}: {root: Node}) {
// now you can use `root` and it is of type Node
}
你要
function foo({root = document.activeElement } : {root: Node}) {
root;// Okay
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.