microsoft / microsoft/TypeScript
`satisfies` operator could not completely overshadow the existing contextual type
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
🔍 Search Terms
satisfies contextual type implicit any parameters
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
It would be great if satisfies wouldn't completely disassociate the expression from the existing contextual type.
📃 Motivating Example
declare function fn<T extends Record<string, (arg: number) => void>>(
obj: T,
): void;
// this works without problems
fn({
foo: (a) => {},
bar: (a) => {},
});
// but we can't add extra constraints through `satisfies` without breaking existing contextual typing
fn({
foo: (a) => {}, // Parameter 'a' implicitly has an 'any' type.(7006)
bar: (a) => {}, // Parameter 'a' implicitly has an 'any' type.(7006)
} satisfies Record<"foo" | "bar", unknown>);
💻 Use Cases
- What do you want to use this for?
It would be nice, at times, to add some extra constraints in places where the existing contextual type exists
- What shortcomings exist with current approaches?
satisfies breaks existing contextual typing so things like contextual parameters "break". It requires us to type the parameters manually or to introduce identity functions with those extra constraints like below
- What workarounds are you using in the meantime?
This is one of the possible workarounds:
declare function fn<T extends Record<string, (arg: number) => void>>(
obj: T,
): void;
declare function identityConstraint<T extends Record<"foo" | "bar", unknown>>(
arg: T,
): T;
fn(
identityConstraint({
foo: (a) => {},
bar: (a) => {},
}),
);
However, this has an unintended side-effect: excess property check doesn't apply to this object now.
A similar shortcoming related to ThisType and satisfies combination has been recently reported here.
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 damit, das motivierende Beispiel zu reproduzieren und den gewöhnlichen Aufruf mit der Version unter Verwendung von satisfies zu vergleichen. Lies die zugehörige Diskussion in issue 57431 und verfolge, wie kontextuelle Typisierung und satisfies zusammenspielen. Als erledigt gilt die Aufgabe, wenn die zusätzliche Einschränkung erhalten bleibt, ohne implicit-any-Fehler in den Callback-Parametern zu verursachen, während die bestehenden Prüfungen intakt bleiben.
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