microsoft / microsoft/TypeScript

Infer typeof self if unspecified on cast

Aperta
#45,618 11 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non viene indicato alcun file né test. Inizia riproducendo i due esempi di cast ed esaminando come TypeScript gestisce le asserzioni di tipo e i tipi delle espressioni inferiti; confronta l'argomento generico implicito proposto con la forma typeof esistente. Il lavoro sarebbe completato quando ci fossero semantiche concordate, copertura dell'implementazione e test per i casi filter e NonNullable.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.