I have a generic type:
type TResult<T> = {
data: string[];
key: T[];
};
and I want to pass T conditionally, I provided a simple example, I want to set the type depending on the key variable that I get it's value from the provided funtion.
Do you have any idea how I can do that? thanks in advance.
UPDATE!: full-example
type TKey = 'foo' | 'bar' | 'baz'
type TType1 = {
firstName: string
lastName: string
}
type TType2 = {
citry: string
address: string
}
type TType3 = {
age: number
dob: Date
}
type TResult<T> = {
data: string[];
key: T[]
}
function getQuery():TKey {
const keys: TKey[] = ['bar', 'baz', 'foo']
return keys[Math.floor(Math.random() * 3)]
}
const key = getQuery()
const result:TResult< /** I want to set the generic type depending on key */ >