Typescript 你能破坏一个对象并对潜在的未定义常量进行空合并吗?
我使用React,因此我有一个道具对象,例如:Typescript 你能破坏一个对象并对潜在的未定义常量进行空合并吗?,typescript,react-typescript,object-destructuring,nullish-coalescing,Typescript,React Typescript,Object Destructuring,Nullish Coalescing,我使用React,因此我有一个道具对象,例如: const props: { id: number, name?: string} = { id: 1 }; // not defining the `name` const { id, name } = props; // here the `const name` becomes undefined forever and even if I use the defaultProps pattern of React, Typescript s
const props: { id: number, name?: string} = { id: 1 }; // not defining the `name`
const { id, name } = props; // here the `const name` becomes undefined forever and even if I use the defaultProps pattern of React, Typescript still shows warnings for potential undefined
在销毁对象时有没有任何方法可以使用nullish合并?
我无法使用此选项(由于团队定义了ESlint规则):
如果未定义
名称
,则可以指定默认值,而无需使用可选链接:
const { id, name = 'placeholder name' } = props;
我不知道,但它是有效的。谢谢你的回答。我会在10分钟内接受答案
const { id, name = 'placeholder name' } = props;