I don't want to specify the function type every time I call it as I suppose the type can be inferred from the argument somehow. Is it possible?
Here's my current implementation:
export interface Edge<T> {
items: {
value: T;
}[];
}
export function getValuesFromEdge(edge: Edge<T>): T[] {
return edge.items.map(item => item.value);
}
And the errors I'm seeing:
Cannot find name T for
Edge<T>
Cannot find name T for
T[]
You have to because that's the only way to use a generic type for your parameters, but that doesn't mean you have to type each call.
getValuesFromEdge({ items: [{ value: 'SomeValue' }]});
Would work with
export interface Edge<T> {
items: {
value: T;
}[];
}
export function getValuesFromEdge<T>(edge: Edge<T>): T[] {
return edge.items.map(item => item.value);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.