Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot define an arrow function with a single generic type parameter that has a default value with JSX #47062

Closed
giladgo opened this issue Dec 8, 2021 · 6 comments Β· Fixed by #47112
Labels
Bug A bug in TypeScript
Milestone

Comments

@giladgo
Copy link

giladgo commented Dec 8, 2021

Bug Report

πŸ”Ž Search Terms

typescript
jsx
tsx
generic
arrow function
default value

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

import React from 'react'

const myFunc = <T = string,>() => {

}

πŸ™ Actual behavior

The typescript compiler throws multiple errors

πŸ™‚ Expected behavior

I would be allowed to create an arrow function with a single type parameter that has a default value.

Note:

  • This happens only on with JSX enabled
  • Removing the default value (= string) doesn't throw errors
  • Adding a second type parameter before T, ie <K, T=string> doesn't throw errors
@giladgo giladgo changed the title Cannot define an arrow function with a single generic type parameter that has a default value Cannot define an arrow function with a single generic type parameter that has a default value with JSX Dec 8, 2021
@MartinJohns
Copy link
Contributor

MartinJohns commented Dec 8, 2021

Duplicate of Related: #15713.

Workaround: T extends any = string

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 8, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Dec 8, 2021
@RyanCavanaugh
Copy link
Member

We should lookahead one token here for = the same way we do for ,

@giladgo
Copy link
Author

giladgo commented Dec 15, 2021

Amazing! thanks :)

@florianmatz
Copy link

I'm to dumb to use it. @giladgo Could you provide a example syntax?

@MartinJohns
Copy link
Contributor

@florianmatz What do you mean? This works now: const myFunc = <T = string>() => { }

Make sure you're using TypeScript 4.6.2.

@AndreaEsposit
Copy link

I am experiencing the same problem on Typescript 5.1.3. Is it just me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants