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:
xxxxxxxxxx
1
type Parser<T extends unknown> = (arg: string) => T;
2
3
const parseText: Parser<string[]> = (text: string) => {
4
return text.split(',');
5
};
6
7
console.log(parseText('John,25,student'));
Alternative solutions:
xxxxxxxxxx
1
type Parser<T extends any> = (arg: string) => T;
2
type Parser<T extends {}> = (arg: string) => T;