如何通过接口导出自定义类型,然后导出到打字稿中的 children?
How to export custom types through an interface and then to a children in typescript?
我要做的是首先在 class 定义中使用定义的自定义类型(在其选项界面中)。
目前,我收到“无法在我的 Parent 的私有 colors
属性定义中找到名称 'colorsType'。
然后我想知道如何将此 colorsType
自定义类型传递给 children object。这是可能的还是每次我想使用它时都需要重新定义自定义类型?这是我的代码结构的一个例子:
ParentObject如
export class Parent {
private colors: colorsType[] = [];
private property2: string;
constructor(options: ParentOptions) {
this.colors = options.colors;
this.property2 = options.property2;
}
}
有定义的接口:
export interface ParentOptions {
colors: colorsType[];
property2: string;
}
export type colorsType =
| "blue"
| "yellow"
| "red"
| "black";
export default ParentOptions
然后他的children:
export class Children extends Parent {
private property3: number;
constructor(options: SomeOtherOptions)
super(options);
this.property3 = options.property3;
}
这样我就可以将 children object 实例化为:
let object = new Children({
colors: ["blue", "yellow"], //being of colorsType[]
property2: "foo",
property3: 42
});
您可以使用 ConstructorParameters
类型从 class 的构造函数中获取参数的类型,然后提供 class 定义。使用它,您可以形成一个交集,将来自 Parent
构造函数参数的对象与 { property3: number }
连接起来,以允许 Child
class 中的额外 属性 .
export interface ParentOptions {
colors: colorsType[];
property2: string;
}
export type colorsType =
| "blue"
| "yellow"
| "red"
| "black";
export class Parent {
private colors: colorsType[] = [];
private property2: string;
constructor(options: ParentOptions) {
this.colors = options.colors;
this.property2 = options.property2;
}
}
export class Children extends Parent {
private property3: number;
constructor(options: ConstructorParameters<typeof Parent>[0] & { property3: number }) {
super(options);
this.property3 = options.property3;
}
}
let object = new Children({
colors: ["blue", "yellow"],
property2: "foo",
property3: 42
});
我要做的是首先在 class 定义中使用定义的自定义类型(在其选项界面中)。
目前,我收到“无法在我的 Parent 的私有 colors
属性定义中找到名称 'colorsType'。
然后我想知道如何将此 colorsType
自定义类型传递给 children object。这是可能的还是每次我想使用它时都需要重新定义自定义类型?这是我的代码结构的一个例子:
ParentObject如
export class Parent {
private colors: colorsType[] = [];
private property2: string;
constructor(options: ParentOptions) {
this.colors = options.colors;
this.property2 = options.property2;
}
}
有定义的接口:
export interface ParentOptions {
colors: colorsType[];
property2: string;
}
export type colorsType =
| "blue"
| "yellow"
| "red"
| "black";
export default ParentOptions
然后他的children:
export class Children extends Parent {
private property3: number;
constructor(options: SomeOtherOptions)
super(options);
this.property3 = options.property3;
}
这样我就可以将 children object 实例化为:
let object = new Children({
colors: ["blue", "yellow"], //being of colorsType[]
property2: "foo",
property3: 42
});
您可以使用 ConstructorParameters
类型从 class 的构造函数中获取参数的类型,然后提供 class 定义。使用它,您可以形成一个交集,将来自 Parent
构造函数参数的对象与 { property3: number }
连接起来,以允许 Child
class 中的额外 属性 .
export interface ParentOptions {
colors: colorsType[];
property2: string;
}
export type colorsType =
| "blue"
| "yellow"
| "red"
| "black";
export class Parent {
private colors: colorsType[] = [];
private property2: string;
constructor(options: ParentOptions) {
this.colors = options.colors;
this.property2 = options.property2;
}
}
export class Children extends Parent {
private property3: number;
constructor(options: ConstructorParameters<typeof Parent>[0] & { property3: number }) {
super(options);
this.property3 = options.property3;
}
}
let object = new Children({
colors: ["blue", "yellow"],
property2: "foo",
property3: 42
});