You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exporttypeFoo={foo: string;};exporttypeConstraint<FextendsFoo>={part1(): F;part2(p1: F): void;};exporttypeConstraint2<FextendsFoo>={part1(): F;part2(p1: NoInfer<F>): void;};// Replacing `Constraint` with `Constraint2` has no differenceexportconstinferStuff=<FextendsFoo>(thing: Constraint<F>)=>{returnthing;};// This correctly infers the type parameter to have both the `foo` and `bar` keys ...constcorrect=inferStuff({part1(){return{foo: "bar",bar: "baz",};},part2(p1){},});// ... while this does not ...constincorrect=inferStuff({part2(p1){},part1(){return{foo: "bar",bar: "baz",};},})// ... but this does again ?constwhatIsHappening=inferStuff({part2(){},part1(){return{foo: "bar",bar: "baz",};},})
🙁 Actual behavior
When part1 appears before part2, or when the parameter to part2 is omitted, then the type parameter is properly inferred. When part2 appears before part1 with a parameter, then the type parameter is inferred incorrectly, even when using Constraint2 which has NoInfer on that parameter's type.
In 4.6.4 and below, correct has the same type as incorrect (the order of the keys in the object didn't matter, but inferred less specifically).
🙂 Expected behavior
All three usages should be equivalent to each other, and in particular, should infer as Constraint<{ foo: string; bar: string; }>.
Additional information about the issue
Here's another example, that shows relatedly incorrect behavior: playground
The text was updated successfully, but these errors were encountered:
This is a known~ limitation of the intra-expression site inference that was introduced in TS 4.7 (here). For it to work the producer has to come before the consumer.
🔎 Search Terms
"object key order matters"
"infer first occurrence"
🕗 Version & Regression Information
⏯ Playground Link
playground
💻 Code
🙁 Actual behavior
When
part1
appears beforepart2
, or when the parameter topart2
is omitted, then the type parameter is properly inferred. Whenpart2
appears beforepart1
with a parameter, then the type parameter is inferred incorrectly, even when usingConstraint2
which hasNoInfer
on that parameter's type.In 4.6.4 and below,
correct
has the same type asincorrect
(the order of the keys in the object didn't matter, but inferred less specifically).🙂 Expected behavior
All three usages should be equivalent to each other, and in particular, should infer as
Constraint<{ foo: string; bar: string; }>
.Additional information about the issue
Here's another example, that shows relatedly incorrect behavior: playground
The text was updated successfully, but these errors were encountered: