microsoft / microsoft/TypeScript

Narrow typeof x === 'object' to Record<string | number | symbol, unknown> | null | unknown[]

Aperta
#38,801 9 commenti 45 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

typeof object narrow record

Suggestion

When narrowing a variable with typeof x === 'object', narrow to Record<string | number | symbol, unknown> | null | unknown[] instead of object | null.

Use Cases

When testing to see if something is an object, you almost always are starting out with unknown or similarly near-top level type and trying to narrow down to a lower "useful" type. When you use the typeof x === 'object' narrowing operation, currently TypeScript over narrows to the extremely constrained {} | null type. This suggestion would change that behavior to narrow down to the much wider Record<string | number | symbol, unknown> | null | unknown[], which the user can then choose to narrow more if they like, or they can choose to work with it as-is.

Examples

declare const apple: unknown
if (typeof apple !== 'object') throw new Error() // actual: object | null; desired: Record<string|number|symbol, unknown> | null | unknown[]

// uncomment this line and comment out the lines above to see desired behavior
// declare const apple: Record<string|number|symbol, unknown> | null | unknown[]

if (apple === null) throw new Error() // actual: object; desired: Record<string|number|symbol, unknown> | unknown[]
if (Array.isArray(apple)) throw new Error() // actual: object; desired: Record<string|number|symbol, unknown>
for (const key in apple) {
    // Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
    //   No index signature with a parameter of type 'string' was found on type '{}'.
    apple[key] // actual: error; desired: 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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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

Inizia riproducendo gli esempi TypeScript dell’issue e confrontando il narrowing effettivo con il risultato richiesto. Traccia quindi il percorso del controllo dei tipi responsabile del narrowing di typeof object, poi aggiungi o aggiorna la coverage per i casi mostrati con null, array e accessi indicizzati; il lavoro è completato quando questi esempi vengono ristretti all’unione richiesta senza modificare l’output a runtime.

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
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.