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?

3
  • 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 Commented Jan 14, 2019 at 2:54
  • @Lex do mean the former (i.e. 3.2.2) accepts without extends {} but the latter doesn’t? Commented Jan 14, 2019 at 8:48
  • Yeah 3.2.2 accepts it without extends {} not exactly sure when this change was introduced Commented Jan 14, 2019 at 22:20

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.