【发布时间】:2017-11-01 06:11:00
【问题描述】:
是否可以使用泛型传递一个对象,不仅作为类型,而且作为键?
interface store {
category: string
}
interface ajaxResponse<T> {
data: ajaxData<T>
errors: string[]
success: boolean
}
interface ajaxData<T> {
rewards: {
amount: number
error: string
}
T: T
}
function get<T>(url): Promise<ajaxResponse<T>> {
// make ajax GET request
// return json object
}
let res = await get<store>('/my/url')
res.data.store.category
当我这样做时,它会说
“ajaxData”类型上不存在属性“store”
有没有办法让我访问该属性?它一旦编译就可以工作,因为所有内容都被删除了,但我怎样才能让它在编辑器中工作?
编辑
想了想,我觉得我不希望接口和属性一样。我想我会想使用名称为x 的interface,所以可能是这样的:
let res1 = await get<storeA, 'category'>('/my/url1')
let res2 = await get<storeB, 'catList'>('/my/url2')
res1.data.category.category
res2.data.catList.categories
【问题讨论】:
-
它“有效”是因为
T: T声明了一个名为T的属性 - 属性名称不会发生通用参数替换 -
没有参数替换是什么意思?你的意思是我没有做一个,还是一个不存在?
-
我在您的代码中看到了
res.data.store.category,我想您希望ajaxData<T> ... { T: T}在您将store传递为T时扩展为{store: store}。实际发生的是它被扩展为{T: store}。 -
是的,我可以在编辑器中调用
res.data.T.category,但这会在运行时中断 javascript。有什么建议吗? -
那么你应该有一些代码在 ajaxData 中初始化
T,当你调用get<store>时,它的值符合store接口,为此你可能需要通过构造函数或get的普通参数以及泛型类型参数<store>。 Interfaces and generics only describe shape of objects for the compiler to do typechecking, they have no effect at runtime.
标签: javascript typescript