Imagine having a collection of flags stored in an object like the example below:
type Flags = {
flag1: string,
flag2: string,
flag3: boolean,
flag4: number
}
// const myFlags: Flags = {
// flag1: 'value 1',
// flag2: 'value 1',
// flag3: true,
// flag4: 12
// }
My goal is to create a function getFlag
with this structure:
function getFlag(flag: keyof Flags): any {
// return myFlags[flag]
}
Instead of simply returning any
, how can I determine and return the specific data type corresponding to the flag being passed into the getFlag
function?
(for simplicity, let's assume the flag types are limited to boolean
, string
, and number
, but the goal is still to extract the exact property type instead of boolean | string | number
)