Top community members

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

[Edit]
EN

React / TypeScript - generic function type in *.tsx

3 points
Created by:
AnnLen
1662

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;

 

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more