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
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
CodePudding user response:
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));
}
CodePudding user response:
If you don't want to use '&' you extend the type, you can make the function generic:
const test = <T extends Template>(template: T[]) => {
template.map(t => console.log(t.d))
}