【发布时间】:2018-05-30 13:58:08
【问题描述】:
来自https://www.typescriptlang.org/docs/handbook/advanced-types.html的示例
function getProperty<T, K extends keyof T>(o: T, name: K): T[K] {
return o[name]; // o[name] is of type T[K]
}
咖喱版:
function curriedGetProperty<T, K extends keyof T>(name: K): (o: T) => T[K] {
return (o: T) => o[name]; // o[name] is of type T[K]
}
const record = { id: 4, label: 'hello' }
const getId = curriedGetProperty('id') // Argument of type '"id"' is not assignable to parameter of type 'never'.
const id = getId(record)
【问题讨论】:
标签: typescript generics currying