Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript TSX and generic parameters

Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones:

const f = <T1>(arg1: T1) => <T2>(arg2: T2) => {    return { arg1, arg2 }; } 

I wonder is there a way to make it work inside a *.tsx file?

like image 355
shadeglare Avatar asked Sep 21 '15 13:09

shadeglare


People also ask

How do you pass a generic type as parameter TypeScript?

Assigning Generic ParametersBy passing in the type with the <number> code, you are explicitly letting TypeScript know that you want the generic type parameter T of the identity function to be of type number . This will enforce the number type as the argument and the return value.

Does TypeScript support generics?

TypeScript supports generic classes. The generic type parameter is specified in angle brackets after the name of the class. A generic class can have generic fields (member variables) or methods.

How do generics work in TypeScript?

The TypeScript documentation explains Generics as “being able to create a component that can work over a variety of types rather than a single one.” Great! That gives us a basic idea. We are going to use Generics to create some kind of reusable component that can work for a variety of types.

How does typescript infer the type of a generic parameter?

TypeScript can also infer the type of the generic parameter from the function parameters. Generics can be used to create generalized classes, like Map. TypeScript can also infer the type of the generic parameter if it's used in a constructor parameter.

What are default parameters in typescript?

Summary: in this tutorial, you will learn about TypeScript default parameters. JavaScript supported default parameters since ES2015 (or ES6) with the following syntax: In this syntax, if you don’t pass arguments or pass the undefined into the function when calling it, the function will take the default initialized values for the omitted parameters.

Does TypeScript support JSX syntax?

Bookmark this question. Show activity on this post. Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones:

Can typescript use angle brackets for type assertions?

Since TypeScript also uses angle brackets for type assertions, combining it with JSX’s syntax would introduce certain parsing difficulties. As a result, TypeScript disallows angle bracket type assertions in .tsx files. Since the above syntax cannot be used in .tsx files, an alternate type assertion operator should be used: as .


Video Answer


2 Answers

This is a result of parsing ambiguity issues. One thing that would make this unambiguous is adding an explicit constraint on T1.

const f = <T1 extends unknown>(arg1: T1) => {     return { arg1 }; } 

Type parameters like T1 implicitly have a constraint of unknown anyway, so in this case your code is functionally equivalent.

Take this solution and you can apply on each arrow function from your original example.

like image 32
Daniel Rosenwasser Avatar answered Oct 04 '22 05:10

Daniel Rosenwasser


To get it to understand it's a type parameter and not a JSX element, you can add a comma after the type parameter:

const f = <T1,>(arg1: T1) => <T2,>(arg2: T2) => {    return { arg1, arg2 }; }; 

Or you could use function expressions instead:

const f = function<T1>(arg1: T1) {     return function<T2>(arg2: T2) {         return { arg1, arg2 };     }; }; 

Or alternatively, in this scenario, this works:

const f = <T1, T2>(arg1: T1) => (arg2: T2) => {    return { arg1, arg2 }; }; 
like image 188
David Sherret Avatar answered Oct 04 '22 04:10

David Sherret