Inner inference doesn't inherit the contextual type from the outer one in argument position
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 42/100
- Issue-Typ
- Bug
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- typescript
- Bereich
- compilers
Rechercherichtung
Beginnen Sie in src/compiler/checker.ts bei inferTypeParameters und der damit verknüpften Behandlung kontextueller Typen rund um getContextualTypeForObjectLiteralElement. Reproduzieren Sie das Problem mit dem bereitgestellten TypeScript Playground-Code und verfolgen Sie dann, warum der verschachtelte Aufruf unknown statt Pattern<'a' | 'b'> erhält; abgeschlossen ist die Arbeit, wenn sowohl primitive als auch Objektargumentformen konsistent inferiert werden, ohne den Userland-Workaround.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Bug Report
🔎 Search Terms
contextual type nested inference
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
const matcher = Symbol("@ts-pattern/matcher");
type MatcherProtocol<input> = {
match: <I>(value: I | input) => void;
};
interface Matcher<input> {
[matcher](): MatcherProtocol<input>;
}
type Pattern<a> =
| Matcher<a>
| (a extends readonly [any, ...any]
? { readonly [index in keyof a]: Pattern<a[index]> }
: a extends object
? { readonly [k in keyof a]: Pattern<a[k]> }
: a);
type Match<i> = {
with<p extends Pattern<i>>(pattern: p): void;
};
declare function match<input>(value: input): Match<input>;
declare function union<input, ps extends [Pattern<input>, ...Pattern<input>[]]>(
...patterns: ps
): Matcher<input>;
declare function when<input, p extends (value: input) => unknown>(
predicate: p
): Matcher<input>;
// those have broken inferences
match<"a" | "b">("a").with(union("a"));
match<"a" | "b">("a")
// @ts-expect-error
.with(union("this is wrong"));
match<"a" | "b">("a").with(
// this should not be an error, since `x` should be `'a' | 'b'` and not `unknown`
when((x) => { let a: "a" | "b" = x; return x; })
);
// those have correct inferences
match<{ type: "a" | "b" }>({ type: "a" }).with({
type: union("a"),
});
match<{ type: "a" | "b" }>({ type: "a" }).with({
// @ts-expect-error
type: union("this is wrong"),
});
match<{ type: "a" | "b" }>({ type: "a" }).with({
type: when((x) => { let a: "a" | "b" = x; return x; }),
});
🙁 Actual behavior
In cases with match<"a" | "b">("a").with(...) the inference is broken whereas in cases with match<{ type: "a" | "b" }>({ type: "a" }).with(...) things work as expected.
🙂 Expected behavior
Both cases should work the same as the position in the with's argument should not matter.
This is a distilled case from ts-pattern by @gvergnaud . The real thing (equivalent of this repro case) can be tested out in this TS playground.
In the broken case, in inferTypeParameters (for the nested call):
- the
contextualTypeisp - the
inferenceTargetTypeisMatcher<input> - and the
instantiatedTypegets calculated asneversince it's using theouterMapperwithInferenceFlags.NoDefault. This in turn assignssilentNeverTypeto theinferredTypehere - all of this leads to inferring
unknownwhen a better inference could have been found (Pattern<'a' | 'b'>)
In the working case, within the same inferTypeParameters we get those:
inferenceTargetType->Matcher<input>contextualType->Pattern<"a" | "b">instantiatedType:Pattern<"a" | "b">
It's worth noting down that outerMapper is the same in both cases (p -> never) but in the working case it's simply not used because the contextualType has no type variables so instantiateTypeWithAlias returns early with the supplied argument here (Pattern<"a" | "b">).
Looking at the previous steps we can learn that the returned contextualType here is better in the working case because getContextualTypeForObjectLiteralElement checks getApparentTypeOfContextualType and there:
contextualType->pinstantiatedType->papparentType->Pattern<{ type: "a" | "b"; }>
Thanks to that the getTypeOfPropertyOfContextualType can return Pattern<"a" | "b"> here in the getContextualTypeForObjectLiteralElement.
It's also worth noting that the current version of ts-pattern works OK because we can "observe" this whole silentNeverType in the userland (which, I think, shouldn't be possible) here and we can return there what we originally expected there to be computed for us. The minimal repro case from this issue with this "hack" being applied can be found here
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus microsoft/TypeScript
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
microsoft/TypeScript#64322 · 2 Kommentare · 1 Reaktion · 2 zugewiesene Personen ·
-
Possible Improvement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
microsoft/TypeScript#64278 · 1 Kommentar · 1 Reaktion ·
-
Docs
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
microsoft/TypeScript#64118 · 1 Kommentar ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
microsoft/TypeScript#64094 ·
-
Docs
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
microsoft/TypeScript#63959 · 5 Kommentare ·
Alle Issues in microsoft/TypeScript
Ähnliche Issues
-
optimization optimization:agents-md-curator
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
githubnext/gh-aw-cao#13143 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
blinklabs-io/bursa#904 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 Kommentare ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 90/100