microsoft / microsoft/TypeScript

Allow identifying readonly properties in mapped types

Aperta
#31,581 1 commento 19 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

Pretty much the ones in the title...

Suggestion

Any TypeScript modifier should be allowed to be easily identified in mapped types. In particular, readonly should, but possibly others too, such as ?.

Use Cases

Define mapped types where the values depend on whether the property is readonly or not. At the moment, the following madness is necessary for this:

 type Or<A extends boolean, B extends boolean> = A extends true
    ? true
    : B extends true
        ? true
        : false

type Equals<X, Y> =
    (<T> () => T extends X ? 1 : 2) extends
        (<T> () => T extends Y ? 1 : 2)
            ? true
            : false

type IsReadonly<O extends Record<any, any>, P extends keyof O> =
    Not<Equals<{[_ in P]: O[P]}, {-readonly [_ in P]: O[P]}>>

I would like to see some syntactic sugar similar to (not intended to be a full implementation):

type Writable<T> = {
    [P in keyof T]: isreadonly T[P] ? never : T[P]
}

In my case, I would like to define a hyperscript-style function which accepts a series of attributes for the creation of HTML elements. For this, I reuse the interfaces that TypeScript defines, such as HTMLAnchorElement, but obviously readonly properties (among others) need to be excluded.

Other people seem to have similar use cases, e.g. https://stackoverflow.com/questions/52443276/how-to-exclude-getter-only-properties-from-type-in-typescript

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 esaminando come TypeScript rappresenta i tipi mappati e i modificatori delle proprietà nel suo compilatore. Confronta la sintassi proposta per il rilevamento di readonly con il comportamento esistente dei tipi mappati e i relativi casi d’uso dei modificatori. Il lavoro sarà completato quando saranno definiti un design condiviso e un comportamento del sistema di tipi per identificare le proprietà readonly, con copertura degli scenari di tipi mappati descritti.

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
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.