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?
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.
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.
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.
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.
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.
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:
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 .
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.
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 }; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With