'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 | number is 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> 

https://www.typescriptlang.org/play?ssl=14&ssc=1&pln=1&pc=1#code/C4TwDgpgBAChBOBnA9gOygXigbwFBQKlQEMBbCALikWHgEtUBzAGn0OMcqIFdSAjBLgC+uXKEhQAasQA23CAHkAZgB4AKgD5MUNQG0A1hBDIlOgLoBuUeOgANbdLmLVcJGg2iAxmhpQl6LAAKUhBHeSow5xVXFFQNAEoI2XllaIRYrQwtPEIoeAhgbnh0EMjhUSA



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source