实现含DeepPartial返回值的接口时无代码操作可用的TS版本问题
问题:TypeScript接口实现代码操作失效,关联DeepPartial与同类型多属性类型
问题场景
实现自动生成的接口时遇到限制:无法修改接口或其类型定义。当接口的函数返回类型为DeepPartial<T>,且T包含两个同类型属性(比如{nested1: Nested, test2: Nested},其中Nested是自定义接口)时,TypeScript的代码操作(自动实现接口的code actions)完全不可用。
复现代码
// 来自ts-proto生成的类型定义 type Builtin = | Date | Function | Uint8Array | string | number | boolean | undefined; export type DeepPartial<T> = T extends Builtin ? T : T extends Array<infer U> ? Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> } : Partial<T>; // 测试接口实现的代码操作 interface Nested { field: string; } interface Foo { request(): DeepPartial<{ nested1: Nested; test2: Nested }>; } interface Bar { request(): DeepPartial<{ nested: Nested }>; } // 无代码操作可用 export class FooImpl implements Foo {} // 代码操作正常可用 export class BarImpl implements Bar {}
版本排查
通过TS Playground测试不同版本后确认:该问题仅在TypeScript版本>4.3.5时出现,4.3.5及更早版本中代码操作正常。
疑问与求助
想确认:
- 是否可以通过调整
tsconfig.json的配置项解决这个问题? - 这是否是TypeScript 4.3.5之后引入的bug,需要向官方上报?
我认为当前的DeepPartial类型定义和接口实现逻辑没有本质问题,希望得到相关解决方案或判断。
内容的提问来源于stack exchange,提问作者Lucas Mähn
相关产品推荐
相关产品推荐

