microsoft / microsoft/TypeScript
Add alternative to `extends` that performs shallow excess property checking
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit den extends!-Beispielen des Vorschlags und dem bestehenden Verhalten der Prüfung auf überzählige Eigenschaften, das für Typentests und Props von React-Komponenten beschrieben ist. Definiere die beabsichtigte Semantik und die Randfälle des Operators und überprüfe anschließend, dass die Beispiele die gewöhnliche Zuweisbarkeit von einer flachen Prüfung auf überzählige Eigenschaften unterscheiden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100