microsoft / microsoft/TypeScript

No way to type an object with null prototype

Aperta
#1,108 24 commenti 42 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Currently the base type of all objects in TS seems to be {}, which looks like an empty type but actually has members inherited from the Object object. This leaves no way to express a type with null prototype.

var foo: ??? = Object.create(null);
foo.toString(); // Want a compile error here

I did accidentally discover that it's possible to type a variable as void:

var foo: void = Object.create(null);

This appears to suppress intellisense for members in VS and the playground, and it is assignable to any (Object.keys(foo) compiles), so it would seem to be what I'm looking for. However this still lets foo.toString() compile somehow - I imagine the void type is getting elevated to Object automatically.

Edit: Ryan's workaround to subtype Object and override all its members to void prevents those properties from being called as functions, but as he points out, that doesn't prevent them from being accessed.

var bar = foo.toString; // Want a compiler error here too. Won't get one with Ryan's workaround.

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

Riproduci gli esempi di Object.create(null) nel TypeScript playground, quindi traccia come vengono modellati {}, void e i membri di Object. Il lavoro è completato quando un tipo supportato può rappresentare un oggetto con prototipo null e rifiuta entrambi gli esempi di accesso ai membri. L’issue non indica alcun file o test da usare come obiettivo.

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

Valutazione

Stack tecnologico
javascript, 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
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.