Avatar ·

重写在 d.ts 文件中声明的接口属性类型

大家好!我是 TypeScript 新手,在使用一个带有类型声明文件 (d.ts) 的库时遇到了问题。假设我有一个来自库的接口: ```typescript // node_modules/some-library/index.d.ts export interface User { id: number; name: string; email: string; age: number; } ``` 我需要创建自己的接口版本,其中 `id` 字段是 string 而不是 number。我尝试继承接口并重写属性类型,但 TypeScript 报错: ```typescript import { User } from 'some-library'; // 尝试 1:直接继承并重写 interface MyUser extends User { id: string; // 错误:Type 'string' is not assignable to type 'number' } // 尝试 2:创建同名新接口(声明合并) interface User { id: string; // 同样错误:Subsequent property declarations must have the same type } ``` 如何正确更改来自库的接口中 `id` 属性的类型?