interface Music {
id: number
}
interface AMusic extends Music{
title: string
}
// please see `selectors` variable below
export type KVPairs<T, K extends keyof T = keyof T> = K extends keyof T
? { key: K; getValue(): T[K] | undefined }
: never;
type Selector = KVPairs<AMusic>
const selectors: Selector[] = [
{key: 'id', getValue: () => 1},
{key: 'title', getValue: () => "hahaha"}
]
const m: Partial<AMusic> = {}
for(const selector of selectors) {
m[selector.key] = selector.getValue()
}
When assigning selector.getValue() to m[selector.key], below type error occurs.
Type 'string | number | undefined' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.(2322)
I know selector.getValue() and m[selector.key] has same type. How can I tell the typescript compiler about this?