Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 你能破坏一个对象并对潜在的未定义常量进行空合并吗?_Typescript_React Typescript_Object Destructuring_Nullish Coalescing - Fatal编程技术网

Typescript 你能破坏一个对象并对潜在的未定义常量进行空合并吗?

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

我使用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 still shows warnings for potential undefined
在销毁对象时有没有任何方法可以使用nullish合并? 我无法使用此选项(由于团队定义了ESlint规则):


如果未定义
名称
,则可以指定默认值,而无需使用可选链接:

const { id, name = 'placeholder name' } = props;

我不知道,但它是有效的。谢谢你的回答。我会在10分钟内接受答案
const { id, name = 'placeholder name' } = props;