I'm trying to create an object interface (transforms), that has keys which are properties of a generic object (T), so that the keys can map to a function that passes the property value of T specific to the key it was passed.
interface Options<T> {
headers: (keyof T)[]
filename: string
items: T[]
transforms?: Partial<{
[field in T as keyof T]: (item: T[field extends keyof T ? field : never]) => string
}>
}
As of now, item always resolves to never. I want it to be able to resolve to T[field] or rather T[keyof T] specific to the key it's attached to.
EDIT
Here is a better example of what I was trying to accomplish. I'm trying to make sure that the arguments passed to the functions in the transforms object, which have keys that are properties of T, have the correct type (T[keyof T]). In the corrected example below, color should be a string, and isTasty should be a boolean when passed as arguments in "transforms".
interface Food {
isTasty: boolean
color: string
}
interface Options<T> {
headers: (keyof T)[]
filename: string
items: T[]
transforms?: Partial<{
?
}>
}
function Foo<T>({headers, filename, items, transforms}: Options<T>){
return 'bar'
}
Foo({
headers: ['isTasty', 'color'],
filename: 'test',
items: [{color: 'red', isTasty: true}] as Food[],
transforms: {
color: (color) => '#' + color,
isTasty: (isTasty) => isTasty ? 'Yes' : 'No'
}
})
[field in T as keyof T]: (item: T[field extends keyof T ? field : never]) => string, to the extent where I doubt it's worth explaining. Suffice it to say that it doesn't do what you think it does.interface Foo {a: string, b: number, c: boolean}and then you wantOptions<Foo>to have atransformsproperty of type{a: (item: string) => string; b: (item: number) => string; c: (item: boolean) => string;}. Or any concrete use case that helps to express your intent