'How to return a union from the fn
I the following, X as union type string | number which is correct.
The fn instead has a return type ValueOf<Person>.
I would like to ask you:
- why a union like
string | numberis not returned? - how to make sure the return the union based on the value of
ValueOf<Person>?
Thanks
type Person = {
name: string,
age: number
}
type ValueOf<T> = T[keyof T];
type X = ValueOf<Person> // string | number >>> OK
const fn = (myValue: ValueOf<Person>): ValueOf<Person> => {
return myValue
} // fn is ValueOf<Person>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
