microsoft / microsoft/TypeScript
replace implicit 'any' with 'unknown'
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
noImplicitAny, strict, any, unknown
Suggestion
When enabling "strict": true or "noImplicitAny": true in a project, could we assign unknown to un-inferred/un-typed variables, instead of causing a compiler error? This would be a non-breaking change, since all cases where this would have an effect are already compiler errors.
Use Cases
It would allow avoiding having to specify a type when it's being type-checked anyway. The current implementation forces converted-from-js projects to deal with a lot of compiler errors from the outset (many of which use various runtime type-checking methods so should be happy with implicit unknowns).
Examples
const getMessage = input => typeof input === 'string' ? input : 'hello';
const shortenMessage = message => message.substring(3);
the getMessage example would be an error currently, but would be fine in the new world and the function would have return type string.
the shortenMessage example would have a changed error. instead of Parameter 'message' implicitly has an 'any' type we'd see an error at message.substring of Object is of type 'unknown'. To me that's more intuitive in terms of what the problem actually is. message is unknown so it should have type unknown.
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. new expression-level syntax)
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 d’implémentation ni aucun test ; commencez par suivre le comportement du compilateur pour strict et noImplicitAny à l’aide des deux exemples de l’issue. Le travail est terminé lorsque les variables dont le type n’est pas inféré reçoivent unknown là où cela est proposé, que getMessage reste valide et que le diagnostic pour shortenMessage change comme décrit.
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
- 30/100