microsoft / microsoft/TypeScript
Add alternative to `extends` that performs shallow excess property checking
Nessuno ha ancora preso questa issue.
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- 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