是否有方法更改*.d中定义的接口属性的类型?Ts在typescript中?

例如: x.d.ts中的接口定义为

interface A {
  property: number;
}

我想在我写入的typescript文件中改变它

interface A {
  property: Object;
}

甚至这个也可以

interface B extends A {
  property: Object;
}

这种方法有效吗?当我试我的系统时,它不工作。只是想确认一下有没有可能?


当前回答

如果你只想修改一个现有属性的类型,而不是删除它,那么&就足够了:

// Style that accepts both number and percent(string)
type BoxStyle = {
  height?: string | number,
  width?: string | number,
  padding?: string | number,
  borderRadius?: string | number,
}

// These are both valid
const box1: BoxStyle = {height: '20%', width: '20%', padding: 0, borderRadius: 5}
const box2: BoxStyle = {height: 85, width: 85, padding: 0, borderRadius: 5}

// Override height and width to be only numbers
type BoxStyleNumeric = BoxStyle & {
  height?: number,
  width?: number,
}

// This is still valid
const box3: BoxStyleNumeric = {height: 85, width: 85, padding: 0, borderRadius: 5}

// This is not valid anymore
const box4: BoxStyleNumeric = {height: '20%', width: '20%', padding: 0, borderRadius: 5}

其他回答

不能更改现有属性的类型。

你可以添加一个属性:

interface A {
    newProperty: any;
}

而是改变现有的一种类型:

interface A {
    property: any;
}

导致一个错误:

后续变量声明必须具有相同的类型。变量 'property'必须是'number'类型,但这里有'any'类型

当然,您可以拥有自己的接口来扩展现有的接口。在这种情况下,你可以重写一个类型到一个兼容的类型,例如:

interface A {
    x: string | number;
}

interface B extends A {
    x: number;
}

顺便说一下,你可能应该避免使用Object作为类型,而是使用any类型。

在任何类型的文档中,它声明:

any类型是使用现有JavaScript的强大方式, 允许您在过程中逐渐选择加入或退出类型检查 编译。您可能希望Object扮演类似的角色 在其他语言中是这样。但是Object类型的变量只允许你 要给它们赋值-你不能调用任意的方法 他们,甚至那些真实存在的人:

let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.

稍微扩展一下@zSkycat的回答,您可以创建一个泛型,它接受两种对象类型,并返回一个合并的类型,其中第二个对象类型的成员覆盖第一个对象类型的成员。

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
type Merge<M, N> = Omit<M, Extract<keyof M, keyof N>> & N;

interface A {
    name: string;
    color?: string;
}

// redefine name to be string | number
type B = Merge<A, {
    name: string | number;
    favorite?: boolean;
}>;

let one: A = {
    name: 'asdf',
    color: 'blue'
};

// A can become B because the types are all compatible
let two: B = one;

let three: B = {
    name: 1
};

three.name = 'Bee';
three.favorite = true;
three.color = 'green';

// B cannot become A because the type of name (string | number) isn't compatible
// with A even though the value is a string
// Error: Type {...} is not assignable to type A
let four: A = three;

我已经创建了这个类型,允许我轻松地覆盖嵌套接口:

export type DeepPartialAny<T> = {
  [P in keyof T]?: T[P] extends Obj ? DeepPartialAny<T[P]> : any;
};

export type Override<A extends Obj, AOverride extends DeepPartialAny<A>> = { [K in keyof A]:
  AOverride[K] extends never
    ? A[K]
    : AOverride[K] extends Obj
    ? Override<A[K], AOverride[K]>
    : AOverride[K]
};

然后你可以这样使用它:

interface Foo {
  Bar: {
    Baz: string;
  };
}
type Foo2 = Override<Foo, { Bar: { Baz: number } }>;

const bar: Foo2['Bar']['Baz'] = 1; // number;

对于缩小属性的类型,简单的扩展是完美的,正如Nitzan的回答:

interface A {
    x: string | number;
}

interface B extends A {
    x: number;
}

为了扩大,或通常覆盖类型,你可以做Zskycat的解决方案:

interface A {
    x: string
}

export type B = Omit<A, 'x'> & { x: number };

但是,如果您的接口A扩展了一个通用接口,那么在使用省略时,您将失去A的剩余属性的自定义类型。

e.g.

interface A extends Record<string | number, number | string | boolean> {
    x: string;
    y: boolean;
}

export type B = Omit<A, 'x'> & { x: number };

let b: B = { x: 2, y: "hi" }; // no error on b.y! 

原因是,在内部,省略只在排除<keyof A, 'x'>键,这将是一般字符串|数字在我们的情况下。因此,B将变成{x: number;}并接受数字| string | boolean类型的任何额外属性。


为了解决这个问题,我提出了一个不同的OverrideProps实用程序类型如下:

type OverrideProps<M, N> = { [P in keyof M]: P extends keyof N ? N[P] : M[P] };

例子:

type OverrideProps<M, N> = { [P in keyof M]: P extends keyof N ? N[P] : M[P] };

interface A extends Record<string | number, number | string | boolean> {
    x: string;
    y: boolean;
}

export type B = OverrideProps<A, { x: number }>;

let b: B = { x: 2, y: "hi" }; // error: b.y should be boolean!

覆盖接口的两个或多个属性的解决方案:

接口原始{ 答:字符串; b:字符串; c:字符串; } interface Modified extends省略<Original, 'a' | 'b'> { 一个吗?:字符串;//设置为可选 b:布尔;//设置为布尔值 d:数量;//添加另一个属性 }

来自TypeScript文档