将 class 属性 标记为通用但类型在 Typescript 中暂时未知
Mark class property as a generic but of a type unknown for the moment in Typescript
在下面的接口中,linkedTitle
属性 包含一个 link 到它自己的接口的另一个实例,但 Title<>
的类型可能不同。
export interface Title<T> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitle: Title;
// ^^^^^ Generic type 'Title<T>' requires 1 type argument(s).
name: string;
presentation: string;
type: number;
}
如何将类型传递给linkedTitle
?
只是基于@Matthieu Riegler 的评论。我想我想要的是以下内容。
export interface Title<T = string | number| boolean> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitles: Title;
name: string;
presentation: string;
type: number;
}
虽然以下解决方案也按建议工作
export interface Title<T> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitles: Title<unknown>;
name: string;
presentation: string;
type: number;
}
export interface Title<T = unknown> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitles: Title;
name: string;
presentation: string;
type: number;
}
在下面的接口中,linkedTitle
属性 包含一个 link 到它自己的接口的另一个实例,但 Title<>
的类型可能不同。
export interface Title<T> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitle: Title;
// ^^^^^ Generic type 'Title<T>' requires 1 type argument(s).
name: string;
presentation: string;
type: number;
}
如何将类型传递给linkedTitle
?
只是基于@Matthieu Riegler 的评论。我想我想要的是以下内容。
export interface Title<T = string | number| boolean> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitles: Title;
name: string;
presentation: string;
type: number;
}
虽然以下解决方案也按建议工作
export interface Title<T> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitles: Title<unknown>;
name: string;
presentation: string;
type: number;
}
export interface Title<T = unknown> {
data: {
description: string;
presentation: string;
value: T;
};
linkedTitles: Title;
name: string;
presentation: string;
type: number;
}