-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Generic parameter defaults in arrow functions confused as jsx elements in tsx files #45939
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
Comments
You forgot to fill out the issue template. It asks for search terms you used to find existing issues. Duplicate of #15713. |
Whoops, don't know why I didn't get a template that time. This is a slightly different issue which I show in the ts playground here. Basically, there are workarounds for generics on arrow functions, but not for generic parameter defaults with the // Known similar issue with generics https://github.com/microsoft/TypeScript/issues/15713
//Work arrounds
export const CommaWorkArroundForGenerics = <A,>() => {
return 'ok in ts file';
};
export const ExtendsWorkArroundForGenerics = <A extends unknown>() => {
return 'ok in ts file';
};
export const DontUseArrowFunctionsWorkArround = function <T = string>(x:T) {
return x
};
// I believe a new issue without work arrounds for generic parameter defaults in arrow functions
export const Broke1 = <IX = string,>(x:Ix) => { // no comma workarround
return 'ok in ts file';
};
export const Broke2 = <IX = string extends unknown>(x:Ix) => { // no extends workarround
return 'ok in ts file';
}; |
Your last example gets the syntax wrong in general—the constraint needs to come before the default. It works if you fix that, except that the second-to-last example breaks parsing for everything after it. So the one thing that doesn’t work is an arrow function with a default but no constraint (as in |
Aha, got it, thanks.
|
To replicate, copy that code in a ts file (v3.3+ tested, updated vscode). It should be ok. Rename ts to tsx and the generic parameter default will be considered a jsx element and therefore a syntax error. I discovered this doing some codegen into a tsx file. If we are writing our own code, we can work around, but it seems jsx and generic parameter defaults on arrow functions aren't working together at the moment.
The text was updated successfully, but these errors were encountered: