microsoft / microsoft/TypeScript

Add alternative to `extends` that performs shallow excess property checking

Aperta
#40,180 2 commenti 5 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

excess property check exact strict extends

Suggestion

I'm proposing an extends! operator that does excess property checking.

Use Cases

I specifically would like this for testing types. It would be helpful for having a check like
expectTypeOf<{a: 1, b: 1}>().toBeAssignableTo<{a?: 1}>();

Also for type-checking props of React components, TypeScript uses excess property checking:

<Component propExists propDoesntExist />
                      ^^^^^^^^^^^^^^^

so it's not enough to just check Props extends React.ComponentProps<Component> ? true : false to determine TypeScript would give a type error in this scenario.

I'm currently approximating this by doing something similar to:

Props extends React.ComponentProps<Component>
  ? keyof Props extends keyof React.ComponentProps<Component>
    ? true
    : false
  : false;

Examples

// `true`
type Example1 = {a: 1, b: 1} extends {a?: 1} ? true : false;

// `false`
type Example2 = {a: 1, b: 1} extends! {a?: 1} ? true : false;

// `true`
type Example3 = {a: 1} extends! {a?: 1} ? true : false;
// Type 'keyof T' cannot be used to index type 'U'.(2536)
type Example1<T, U> = T extends U ? U[keyof T] : never;
                                      ^^^^^^^
// Okay
type Example2<T, U> = T extends! U ? U[keyof T] : never;

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 con gli esempi di extends! della proposta e con il comportamento esistente del controllo delle proprietà in eccesso descritto per i test dei tipi e le props dei componenti React. Definisci la semantica prevista dell'operatore e i relativi casi limite, quindi verifica che gli esempi distinguano l'assegnabilità ordinaria dal controllo superficiale delle proprietà in eccesso.

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.