microsoft / microsoft/TypeScript

Infer typeof self if unspecified on cast

Ouverte
#45,618 11 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

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

Description

Suggestion

🔍 Search Terms

  • type casting
  • implicit typing
  • syntactic sugar

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

It would be a lot cleaner if when you cast a type to some other type (for example NonNullable) you don't have to specify typeof self.

📃 Motivating Example

if you have an Array that could be undefined you can filter out undefined like so

const array = ["one", undefined, "two"].filter(Boolean);
console.log(array) // ["one", "two"]

but in this case the type would still be (string | undefined)[] so it would cause linting errors later in the code. While this may be specific to the .filter method, it may be unreasonable to expect all methods accurately account for type checks like this. So one easy way to fix this would just be to do

const arrayUndefined = ["one", undefined, "two"]

const array = arrayUndefined.filter(Boolean) as NonNullable<UnArrayify<typeof arrayUndefined>>

what I'm suggesting is that instead of requiring you do typeof arrayUndefined you can instead do an implicit cast to make the code cleaner

const arrayUndefined = ["one", undefined, "two"]

// it's implied that UnArrayify would be called with <typeof array.filter(Boolean)>
const array = arrayUndefined.filter(Boolean) as NonNullable<UnArrayify>

💻 Use Cases

I would like this because it would result in cleaner typing for unhandled edgecases in the typescript linter

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

Aucun fichier ni test n'est nommé. Commencez par reproduire les deux exemples de cast et examiner comment TypeScript gère les assertions de type et les types d'expression inférés ; comparez l'argument générique implicite proposé avec la forme typeof existante. Pour considérer le travail comme terminé, il faudrait s'être accordé sur la sémantique, couvrir l'implémentation et ajouter des tests pour les cas filter et NonNullable.

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

Recevez les nouvelles issues par e-mail

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