Languages
[Edit]
EN

React / TypeScript - generic function type in *.tsx

3 points
Created by:
AnnLen
1879

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;

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join