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

Can you destruct an object and have nullish coalescing for potential undefined constants?

我用的是React,所以我有一个props对象,例如:

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

有没有办法在销毁对象时使用无效合并? 我不能使用它(由于团队定义的 ESlint 规则):

const name = props.name ?? 'placeholder name';

如果 name 未定义,您可以分配默认值,而不使用可选链接:

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