打字稿传播和复制-速记作业

我有传播和复制代码,我在其中从超级集提取类型。随着属性列表的增长,有没有更优雅的方法?

 const { name, handler, targetGrid, type } = event; //eg: event here is of type any
 let e: Event = {  name, handler, targetGrid, type } as Event;

Complete code: StackBlitz

The closest i could see is a solution here SO. But that would still merge my 2 lines to a single line, and eventually when the params grows big, lint would ask me to break the lines....

我正在寻找一种解决方案,无需重复两次属性名称。

评论
  • reos
    reos 回复

    有点凌乱,但是它可以满足您的要求:

    let e: Event = ["name", "handler", "targetGrid", "type"].reduce((a, c) => (a[c] = event[c], a), {});