here is a playground:
Basically having a function where I can send in an argument that returns some value. It gives me a union however since there are keys with the same name that have different types:
const qu = {
q2: {
obj1: 'test',
obj2: 'test2'
},
q3: {
obj1: 2,
obj2: 234
}}
is there a way to infer the type? I've tried with conditionals but with no luck. Or is this a TS limitation?
Here is full code example:
type q = {
[key: string]: {
obj1: any,
obj2: any
}}
const qu = {
q2: {
obj1: 'test',
obj2: 'test2'
},
q3: {
obj1: 2,
obj2: 234
}}
const queryFn = <T extends q>(obj: T) => <K extends keyof T>(q: K): T[K]['obj1'] => {
const {obj1, obj2} = obj[q]
return obj1
}
const qur = queryFn(qu)
const someotherfn = (arg: Parameters<typeof qur>[0]) => {
let res: ReturnType<typeof qur<typeof arg>>
res = qur(arg)
return res
}
const res = someotherfn('q3') // giving me a union instead of inferring correct type