使用 Typescript 用剩余和类型解构对象
解释器表示 humanProps 的类型为
humanProps: { humanProps: IHumanProps} 。
如何正确设置传播类型,以使 humanPros 具有
IHumanProps 类型?
示例:
interface IName {
name: string
}
interface IHumanProps {
hobby: string,
age: number
}
interface IHuman extends IName, IHumanProps {}
const human: IHuman = {
name: 'Linda',
hobby: 'reading',
age: 99
}
const { name, ...humanProps }: { name: string, humanProps: IHumanProps } = human;
console.log(name);
console.log(humanProps);
如何使用扩展语法解构对象并指定类型?我在网上找不到有关此具体案例的任何信息。
我收到的确切错误是 VSCode(或者更确切地说是 linter)说解构的 humanProps 类型不是 IHumanProps
类型,而是类型 { humanProps: { humanProps: IHumanProps}}
,这是不正确的。我认为这可能是 VSCode/ESLint 的问题。
The interpreter says that humanProps
is of type humanProps: {humanProps: IHumanProps}
.
How can I set the type for the spread correctly so that humanPros
has the type IHumanProps
?
Example:
interface IName {
name: string
}
interface IHumanProps {
hobby: string,
age: number
}
interface IHuman extends IName, IHumanProps {}
const human: IHuman = {
name: 'Linda',
hobby: 'reading',
age: 99
}
const { name, ...humanProps }: { name: string, humanProps: IHumanProps } = human;
console.log(name);
console.log(humanProps);
How can I destructure an object using spread syntax and specify the types? I couldn't find any information online about this specific case.
The exact error I was receiving was that VSCode (or maybe rather the linter) were saying that the deconstructed humanProps
were not of the type IHumanProps
but rather of type {humanProps: {humanProps: IHumanProps}}
, which is not correct. I assume this could be have been an issue with VSCode/ESLint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您重写类型以使其更加清晰
现在,为了破坏
人类
对象,您只需执行以下操作I would suggest you to rewrite the types to be more clear
Now, in order to destruct the
human
object you simply have to do the following