Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript: How to define a function that references an object using generics

How does one define a function based on keys of an object using generics. The actual use case has more keys and values so I prefer not to define it explicitly.

const obj = {
    a: {
        1: 1,
        2: 2,
        3: 3,
    },
    b: {
        1:1,
        2:2,
        4:4
    },
};

// My best attempt
const fn = <T extends keyof typeof obj>(key1: T, key2: keyof T) => {
    return obj[key1][key2];
}

Error: Type 'keyof T' cannot be used to index type '{ a: { 1: number; 2: number; 3: number; }; b: { 1: number; 2: number; 4: number; }; }[T]'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to add some more generic parameters:

const obj = {
    a: {
        1: 1,
        2: 2,
        3: 3,
    },
    b: {
        1:1,
        2:2,
        4:4
    },
};

const fn = <O, T extends keyof O, U extends keyof O[T]>(o: O, key1: T, key2: U) => {
    return o[key1][key2];
}

fn(obj, 'a', 1) // ok
fn(obj, 'a', 6) // error

TS playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading