3

As described in the previous question, What is the syntax for Typescript arrow functions with generics, a few days ago the Typescript compiler would not accept:

const foo = <T>(x: T) => x

The workaround was to use:

const foo = <T extends {}>(x: T) => x

Yesterday however I noticed that when I removed "extends {}", to bring the line back to the first snippet above, the compiler would accept it without any errors. Further testing indicated that the line was working correctly.

Did typescript change this syntax recently, so that arrow functions with generics no longer require "extends {}"?

If so, when did this happen?

Luke
  • 31
  • 2
  • 1
    Did you update Typescript recently? It can't have changed locally unless a different version was installed. I have 3.2.2 and 2.9.2 installed. The later throws that error for me, the former accepts it. So they must have fixed somewhere – Lex Jan 14 '19 at 02:54
  • @Lex do mean the former (i.e. 3.2.2) accepts without `extends {}` but the latter doesn’t? – Shelby Moore III Jan 14 '19 at 08:48
  • Yeah 3.2.2 accepts it without `extends {}` not exactly sure when this change was introduced – Lex Jan 14 '19 at 22:20

1 Answers1

7

Are you in a .ts file?

In a .tsx file, TypeScript decides to parse <T> as a JSX opening tag; however, in a .ts file, JSX isn't permitted and so TypeScript is just fine parsing that as a type parameter.

Daniel Rosenwasser
  • 21,855
  • 13
  • 48
  • 61