microsoft / microsoft/TypeScript

Allow leading underscore for types to bypass noUnusedLocals warning

Ouverte
#58,561 6 commentaires 16 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Help Wanted Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔍 Search Terms

"ts-ignore", "ts-expect-error", "noUnusedLocals", "noUnusedLocals underscore", "noUnusedLocals prefix"

✅ Viability Checklist
⭐ Suggestion

Add support for leading underscore in type definition name to disable tsc noUnusedLocals warning ts(6196), like it does for other things.

📃 Motivating Example

Allows for easier testing of types by providing a way to enabled noUnusedLocals for your project, and selectively opting out for local types used for testing. Prior to this, you had a couple of less than idea solutions:

  1. live with the warnings
  2. disable noUnusedLabels for test code
  3. use @ts-ignore

Depending on the method you're using for testing your types, you might not be able to use @ts-ignore. For example, if you were relying on usage of @ts-expect-error to assert invalid usages of your types in tests, @ts-ignore would also cause typescript not to flag the type error you're trying to assert with @ts-expect-error.

For example, a test like this:

type StringKeyOf<T extends Record<string, unknown>> = T extends T
  ? Extract<keyof T, string>
  : never;

type TestType = { s: unknown; 1: unknown; };

// NOTE: `expectFails` type will get flagged with unused warning, when `noUnusedLocals` is enabled,
//     and `@ts-ignore` is no help because it also suppresses the compile error we want to validate

// @ts-expect-error - validate type error will occur
type expectFails = Expect<Equals<StringKeyOf<TestType>, 's' | 1>>;

Previously, prefixing the local type with underscore would have no affect on the warning, but now you can update the code to include a leading underscore for the type name, and the warning will go away.

// @ts-expect-error
type _expectFails = Expect<Equals<StringKeyOf<TestType>, 's' | 1>>;
💻 Use Cases
  1. What do you want to use this for?
    • I'd like to use to in my type validation tests.
  2. What shortcomings exist with current approaches?
    • The current approaches do not provide a very good way to avoid ending up with a lot of warnings in tests, without disabling noUnusedLocals
  3. What workarounds are you using in the meantime?
    • I'm currently disabling noUnusedLocals in my project's base tsconfig.json and enabling in tsconfig.build.json. This choice was made so the errors would not be present for developers working on test related code, and since the build config does not include tests, it applies the rule against production code. It results in a less than ideal developer experience though, because developers will see errors in build that don't present when viewing the source code, because vscode is using tsconfig.json.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par reproduire l’exemple motivant avec tsc et noUnusedLocals, puis suivez le diagnostic pour le type local inutilisé. Le travail est terminé lorsqu’un trait de soulignement initial supprime ts(6196) pour ce type, tandis que le comportement existant et le JavaScript généré restent inchangés.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.