[英]Typescript nameof generic type T as property
是否可以在類/接口中使用參數 T 的類型作為屬性名稱?
export interface ISample<T> {
nameof(T) : T; <-- this property should have the name of the type T nameof(T)
otherProperties: any;
}
並像這樣使用它:
const data: ISample<Houses>() = <something to construct the object>
console.log(data.houses)
所以houses
是接口生成的data
的屬性。
TypeScript 是結構型系統。 也就是說,類型系統關注的是結構,而不是名稱,並且類型不一定有名稱,更不用說唯一的名稱了。
例如,假設你要寫:
interface Person {
name: string;
}
interface Street {
name: string;
}
是
ISample<{name: string}>
應該有一個person
或street
財產?
所以不,你要的東西不存在。
您可能需要聲明:
interface PersonSample {
person: Person;
}
interface StreetSample {
street: Street;
}
或者
interface Sample<T> {
sample: T;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.