dakiesse
dakiesse

Reputation: 562

How to return interface key types in TypeScript?

I have interface

interface Some {
  key1: string
  key2: number
}

There is a function.

const fn = (key: keyof Some) => {
   return <Some>someObject[key]
}

Сan I get the return type by string key?

fn(‘key1’) // can ts know that it will return `string`?
fn(‘key2’) // can ts know that it will return `number`?

UPDATED:

Thank you @captain-yossarian for the answer! Now I have a bit extended case.

export const getConfig = <T extends EntityInitialConfig, K extends keyof T> (key?: K) => {
  let config: T = window.someObject

  return key ? config[key] : config
}

getConfig('clientKey') // ts shows `string | EntityInitialConfig`

And it's a problem because just need only string. How to do it?

Upvotes: 2

Views: 959

Answers (1)

I assume You are looking for next solution:



const fn = <T extends unknown, K extends keyof T>(obj: T, key: K): T[K] => {
    return obj[key]
}

const result = fn({ name: 'John', age: 42 }, 'name') // string
const result2 = fn({ name: 'John', age: 42 }, 'age') // number

[UPDATE] Your function expects key argument to be undefined, you should defenitely provide default value.

If You are Ok with default value, then you should add function overloadings.


interface EntityInitialConfig {
    age: number;
    name: string;
}

const config: EntityInitialConfig = { age: 42, name: 'John' };

interface GetConfig {
    (key: undefined): EntityInitialConfig['name'] // it is kinda default value
    <K extends keyof EntityInitialConfig>(key: K): EntityInitialConfig[K]

}
export const getConfig: GetConfig = <K extends keyof EntityInitialConfig>(key?: K) => {
    return key ? config[key] : config
}

const x = getConfig('age') // ts shows number

P.S. I'm not sure if it is correct syntax: return <Some>someObject[key]

Upvotes: 3

Related Questions