typescript 打字稿:传播类型只能从对象类型创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51189388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:55:07  来源:igfitidea点击:

Typescript: Spread types may only be created from object types

typescript

提问by Morten Poulsen

function foo<T extends object>(t: T): T {

  return {
    ...t    // Error: [ts] Spread types may only be created from object types.
  }
}

I am aware that there are issues on github, but I can't figure out what is fixed and what is not and they have 2695 open issues. So I am posting here. I am using latest Typescript 2.9.2.

我知道 github 上存在问题,但我无法弄清楚哪些已修复,哪些未修复,并且他们有 2695 个未解决的问题。所以我在这里发帖。我正在使用最新的 Typescript 2.9.2。

Should the above code not work? And how can I fix it if possible?

上面的代码应该不起作用吗?如果可能,我该如何解决?

回答by jmattheis

This is fixed in TypeScript Version 3.2. See Release Notes.

这在 TypeScript 3.2 版中得到了修复。请参阅发行说明



Looks like spread with a generic type isn't supported yet, but there is a GitHub issue about it: Microsoft/TypeScript#10727.

看起来尚不支持具有泛型类型的传播,但有一个关于它的 GitHub 问题:Microsoft/TypeScript#10727

For now you can either use type assertionlike @Jevgenicommented:

现在你可以使用像@Jevgeni评论的类型断言

function foo<T extends object>(t: T): T {
  return { ...(t as object) } as T;
}

or you can use Object.assignwhich has proper type definitions.

或者您可以使用Object.assignwhich 具有正确的类型定义。

function foo<T extends object>(t: T): T {
  return Object.assign({}, t);
}

回答by Muhammad Mabrouk

You can use blank curly brackets {} or an interface like below examples:

您可以使用空白大括号 {} 或类似以下示例的接口:

goodsArray.map(good => {
  return {
    id: good.payload.doc.id,
    ...good.payload.doc.data() as {}
  };
});

or

或者

goodsArray.map(good => {
  return {
    id: good.payload.doc.id,
    ...good.payload.doc.data() as Goods // 'Goods' is my interface name
  };
});

回答by Titian Cernicova-Dragomir

Version 3.2 of Typescript fixed this. The two PRs that improve handling of spread and rest parameters are:

Typescript 3.2 版修复了这个问题。改善传播和休息参数处理的两个 PR 是:

You can try it out now using npm install [email protected].

您现在可以使用npm install [email protected].

With 3.2 your code works as is.

使用 3.2,您的代码按原样运行。

Version 3.2 has been released on November 29th, 2018, you can read more about it here.

3.2 版本已于 2018 年 11 月 29 日发布,您可以在此处阅读更多相关信息。

回答by Aathil Ahamed

This answer may help to fix the same problem while using angular and firestoe.

这个答案可能有助于在使用 angular 和 firestoe 时解决同样的问题。

return { id: item.payload.doc.id, ...item.payload.doc.data()} as Employee

return { id: item.payload.doc.id, ...item.payload.doc.data()} as Employee

this on will throw he same error. for fixing you have to change like this one.

这将引发他同样的错误。为了修复,你必须像这样改变。

return { id: item.payload.doc.id, ...item.payload.doc.data() as Employee }

return { id: item.payload.doc.id, ...item.payload.doc.data() as Employee }

回答by Simon_Weaver

If you are still getting this error post version 3.2 then you probably have a type error 'upstream' resulting in an object being unknowninstead of an actual object. For instance if you do { ...getData() }and your getDatafunction has a compile error you can get this error.

如果您在 3.2 版后仍然收到此错误,那么您可能有一个类型错误“上游”,导致对象unknown不是实际对象。例如,如果您这样做{ ...getData() }并且您的getData函数有编译错误,您可能会收到此错误。

So check for all compiler errors because it's probably a red herring.

所以检查所有编译器错误,因为它可能是一个红鲱鱼。