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

How do I extend object type inside function param with typescript?

Let’s say I have a type

type Template = {
    a: string;
    b: string;
    c: string
}

and I want to use it inside a function, but I want to add extra param to it. What would be the best way to implement it?

when I try to extend it, typescript says ? expected

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

here’s how I do it

const test = (template: Template extends { d: string }[]) => {
    template.map(t => console.log(t.d));
}

P.S. I don’t to uses [key:string]: string for this type

>Solution :

It’s not possible to extend types.
Ref this accepted answer

You can fix your problem something like this

type Template = {
    a: string;
    b: string;
    c: string
}

type NewTemplate = Template & { d: string }

const test = (template: NewTemplate[]) => {
    template.map(t => console.log(t.d));
}
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