EN
React / TypeScript - generic function type in *.tsx
3
points
In this short article we would like to show how to create generic type that can be used as arrow function type in *.tsx
/ Extended TypeScript.
Quick solution:
type Parser<T extends unknown> = (arg: string) => T;
const parseText: Parser<string[]> = (text: string) => {
return text.split(',');
};
console.log(parseText('John,25,student'));
Alternative solutions:
type Parser<T extends any> = (arg: string) => T;
type Parser<T extends {}> = (arg: string) => T;