三点语法中最干净有效的TypeScript等价物是什么?

时间:2016-10-27 11:07:48

标签: javascript typescript syntax

我正在尝试在我的TypeScript / React代码中过滤掉props,但是其余/展开属性(三点语法...)是not supported in TypeScript yet

const NavItemPass = (props) => {
    const { active, activeKey, activeHref, ...rest } = props;
    return <NavItem {...rest} />;
};

TypeScript 1.8中最干净的不可变等效语法是什么?

1 个答案:

答案 0 :(得分:2)

Babel通过创建一个创建新对象的函数来完成它。您可能希望这样做,制作一个可重用的,然后使用它。

我将在JavaScript中给出一个示例,您可以轻松将其转换为TypeScript:

function objectWithout(obj, keys) {
    let target = {};
    Object.keys(obj).forEach(k => {
        if (keys.indexOf(k) == -1) {
            target[k] = obj[k];
        }
    });
    return target;
}

然后

const NavItemPass = (props) => {
    return React.createElement(NavItem, objectWithout(props, ["active", "activeKey", "activeHref"]));
};

不太满意。 : - |

相关问题