[英]Is there a way to set type for a specific typescript generic type?
例如
interface IWithManyGenericTypes<First, Second, Third, Fourth> {
prop1?: First,
prop2?: Second,
prop3?: Third,
prop4?: Fourth
}
我想為特定的泛型類型設置類型,例如number
,例如:
const someObject: IWithManyGenericTypes<Third = number> = {
prop3: 123
}
有沒有辦法做類似的事情?
在評論中討論了這一點,但只是添加了一個關閉答案。
您還需要以正確的順序處理其他 generics。 如果您不需要它們,可以將它們設置為undefined
或unknown
。
const someObject2:IWithManyGenericTypes<undefined,undefined, number, undefined> = {
prop3: 123
}
為了更好的使用或可讀性,您可以創建一個擴展IWithManyGenericTypes
的其他接口
interface IWithThirdGenericType<Third>
extends IWithManyGenericTypes<undefined, undefined, Third, undefined>{}
const someObject:IWithThirdGenericType<number> = {
prop3: 123
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.