相关疑难解决方法(0)

TypeScript:深偏?

有没有办法在 TypeScript 中指定一个部分类型,它也使所有子对象也成为部分对象?例如:

interface Foobar {
  foo: number;
  bar: {
    baz: boolean;
    qux: string;
  };
}

const foobar: Partial<Foobar> = {
  foo: 1,
  bar: { baz: true }
};
Run Code Online (Sandbox Code Playgroud)

这会引发以下错误:

TS2741: Property 'qux' is missing in type '{ baz: true; }' but required in type '{ baz: boolean; qux: string; }'.

有没有办法使子节点也部分化?

typescript

45
推荐指数
5
解决办法
2万
查看次数

标签 统计

typescript ×1