Languages
[Edit]
EN

React / TypeScript - generic function type in *.tsx

3 points
Created by:
AnnLen
13480

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
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join