Luke
Luke

Reputation: 31

Did the syntax change for Typescript arrow functions with generics?

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?

Upvotes: 3

Views: 344

Answers (1)

Daniel Rosenwasser
Daniel Rosenwasser

Reputation: 23483

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.

Upvotes: 7

Related Questions