简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM