Languages
[Edit]
EN

React / TypeScript - generic function type in *.tsx

3 points
Created by:
Hayley-Mooney
677

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;

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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