Skip to content

Keep trailing comma on arrow function type parameter in mts and cts files #378

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

Closed
sounisi5011 opened this issue Jun 5, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@sounisi5011
Copy link

sounisi5011 commented Jun 5, 2022

Starting with TypeScript 4.5, the following code now throws an error.

export const x = <T>(value: T): T => value;
error TS7060: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma or explicit constraint.

To fix this code, it should be rewritten as follows either:

export const x = <T extends unknown>(value: T): T => value;
export const x = <T,>(value: T): T => value;

dprint formats the second example code to the original unfixed code.

Reference:

@dsherret
Copy link
Member

dsherret commented Jun 5, 2022

There are a few instances where this occurs that dprint-plugin-typescript already handles. In this case, here is the relevant code in TypeScript and it's specific to mts and cts files:

        function checkGrammarArrowFunction(node: Node, file: SourceFile): boolean {
            if (!isArrowFunction(node)) {
                return false;
            }

            if (node.typeParameters && !(length(node.typeParameters) > 1 || node.typeParameters.hasTrailingComma || node.typeParameters[0].constraint)) {
                if (file && fileExtensionIsOneOf(file.fileName, [Extension.Mts, Extension.Cts])) {
                    grammarErrorOnNode(node.typeParameters[0], Diagnostics.This_syntax_is_reserved_in_files_with_the_mts_or_cts_extension_Add_a_trailing_comma_or_explicit_constraint);
                }
            }
        }

Will open a fix soon.

@dsherret dsherret added the bug Something isn't working label Jun 5, 2022
@dsherret dsherret changed the title Exclude single-argument generics arrow functions from "typeParameters.trailingCommas" option Keep trailing comma on arrow function type parameter in mts and cts files Jun 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants