Typescript: Generic arrow functions and JSX

Created on 22 Sep 2015  ·  2Comments  ·  Source: microsoft/TypeScript

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 };
}
By Design Canonical

Most helpful comment

This is an unfortunate side effect of syntax ambiguities. You need to either have an extends clause or multiple type parameters. You can write an extends clause that is functionally equivalent (albeit annoying):

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

All 2 comments

This is an unfortunate side effect of syntax ambiguities. You need to either have an extends clause or multiple type parameters. You can write an extends clause that is functionally equivalent (albeit annoying):

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

That almost worked for me.

To cover all the types including null, I believe it technically should extend any:

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

I think it better communicates that it's no-op too, while extending {} can look like it is a real restriction it wants to enforce.

Was this page helpful?
0 / 5 - 0 ratings