microsoft / microsoft/TypeScript
Codefix: removing inferable types
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
Search Terms
inferable types no-inferable-types no-inferrable-types removal
Suggestion
It is somewhat unnecessary to add : type declarations for member, parameter, and/or variable types already inferable from code.
// This type declaration actually requests a lower amount of type information,
// as `number` is less specific (narrow) than `3`
const value: number = 3;
It'd be nice to have a codefix to remove them to clean up code.
Use Cases
These unnecessary type declarations can easily get introduced in code as it gets changing during editing, and can cause visual clutter.
We should note that some users choose to prefer including these inferable type declarations as a style preference to increase the amount of type information visible purely from source code.
The @typescript-eslint/no-inferable-types already exists with an auto-fixer for a good starting reference.
Examples
Variables such as the above value can generally have their type removed if either is true:
-
The variable is
let, contains an initializer, and the type is the same as what it would be inferred as// We can remove `: number` here let value: number = 0; // We cannot remove `: number | string`, as `| string` is not added by TypeScript let otherValue: number | string = 0; -
The variable is
constand the type is less narrow than what the type is inferred as
Parameters can similarly be treated as let parameters:
// The `: string` can be safely removed
function takesStringWithDefault(input: string = "") { }
Parameters can also have type declarations removed if their parent function is already declared to be a type that provides the same type declaration for the parameter:
type TakesString = (input: string) => void;
// We can remove the `: string` here too
const takesString: TakesString = (input: string) => {};
Class members can be treated as let variables here too.
class WithValue {
// We can remove the `: string` here
value: string = "";
}
Checklist
My suggestion meets these guidelines:
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
L’issue ne nomme aucun fichier du dépôt, test ou point d’entrée. Commencez par examiner les exemples de variables, de paramètres et de membres de classe, puis comparez le comportement proposé avec l’auto-fixer de @typescript-eslint/no-inferable-types ; le travail est terminé lorsqu’un codefix supprime en toute sécurité uniquement les annotations inférables décrites, sans modifier la sortie à l’exécution.
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é
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100