Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conditional type constraint exploration + signature relations #47341

Merged
merged 1 commit into from
Jan 7, 2022

Conversation

ahejlsberg
Copy link
Member

Fixes #47142.

@ahejlsberg
Copy link
Member Author

Adding a regression test isn't simple as the repro references multiple NPM packages.

@ahejlsberg
Copy link
Member Author

To be clear, this PR fixes the repro here #47142 (comment).

Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Individually, these two changes look fine and make sense on their face - the first skips distributive constraints for circularly constrained types, which is good, and the second expands simplified signature matching to instances of the same type reference rather than just instances if the same symbol... which it's weird that the second isn't fully encompassed by the first, which is why an example and repro would be nice, but 🤷‍♂️, looks fine enough, I suppose.

@ahejlsberg
Copy link
Member Author

it's weird that the second isn't fully encompassed by the first

The change to not explore circular constraint is what fixes the stack overflow. Definitely a good change.

The signature relation change keeps us from cross-comparing incompatible signatures. Without it, as we're computing the variance for UpsertGraphMethod (which has multiple signatures), we relate a PartialModelGraph<M> to a PartialModelGraph<M>[] (all are types from the Objection.js NPM package). That then sets off a cascade of deeper and deeper nested complex combinations of conditional types, index access types, and intersections. Deep enough, I guess, that isDeeplyNestedType never sees three cycles and we eventually hit the excessive stack depth limiter.

@ahejlsberg ahejlsberg merged commit a3d23d3 into main Jan 7, 2022
@ahejlsberg ahejlsberg deleted the fix47142 branch January 7, 2022 17:44
Comment on lines 19925 to 19926
// We have instantiations of the same anonymous type (which typically will be the type of a
// method). Simply do a pairwise comparison of the signatures in the two signature lists instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment accurate (i.e. "same anonymous type")?

@sandersn
Copy link
Member

sandersn commented Jan 10, 2022

On DT, this breaks knockout.mapping with

Type 'KnockoutObservable<false> | KnockoutObservable<true>' is not assignable to type 'KnockoutObservable<boolean>'

Is this expected? I'll add more detail after investigating.

Edit: Here's the rest of the stack. Pretty clear the package relied on the broader signature relation before:

Type 'KnockoutObservable<false>' is not assignable to type 'KnockoutObservable<boolean>'.
     Types of parameters 'value' and 'value' are incompatible.
       Type 'boolean' is not assignable to type 'false'.

@sandersn
Copy link
Member

Here's a small repro. I'll file it as a separate bug so we can decide whether it's worthwhile to fix:

interface Kow<T> {
    (value: T): void;
}
declare var src: Kow<true> | Kow<false>;
var tgt: Kow<boolean> = src

@ahejlsberg
Copy link
Member Author

Marking this as a breaking change since the error in #47371 is the intended behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team Breaking Change Would introduce errors in existing code For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stack overflow in TS 4.5.3 and nightly
5 participants