'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
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 1:[1]
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))
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | matt helliwell |
