TypeScript和可选的析构参数

Fre*_*any 3 typescript

我似乎无法获得可选参数与TypeScript中的析构参数一起使用.

为参数生成了正确的代码,但Typescript似乎不允许我在代码中使用生成的变量,从而破坏了目的.

难道我做错了什么?这是一个减少:

declare var lastDirectionWasDownish : boolean;

function goToNext(
    { 
        root: Node = document.activeElement, 
        actLikeLastDirectionWasDownish:boolean = lastDirectionWasDownish
    } = {}
) {
    return root && actLikeLastDirectionWasDownish;
}
Run Code Online (Sandbox Code Playgroud)

汇编成

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;
}
Run Code Online (Sandbox Code Playgroud)

bas*_*rat 7

TypeScript实际上阻止你犯一个你在纯JS中会错过的错误.以下纯JS:

function foo({root: Node}) {
   // the parameter `root` has been copied to `Node`
}
Run Code Online (Sandbox Code Playgroud)

TypeScript理解这一点,并且不允许您使用Node.要添加类型注释,您实际上是:

function foo({root}: {root: Node}) {
   // now you can use `root` and it is of type Node
}
Run Code Online (Sandbox Code Playgroud)

固定

你要

function foo({root = document.activeElement } : {root: Node}) {
    root;// Okay
}
Run Code Online (Sandbox Code Playgroud)