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

Apply inference in method arguments when using a discriminator #60366

Closed
Llorx opened this issue Oct 29, 2024 · 3 comments
Closed

Apply inference in method arguments when using a discriminator #60366

Llorx opened this issue Oct 29, 2024 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@Llorx
Copy link

Llorx commented Oct 29, 2024

🔎 Search Terms

github: interface infer
github: interface discriminator
github: interface discriminator method
google: typescript method gets the result of another optional method
google: typescript one property sets the type of another property
google: typescript one property sets the type of the next property

🕗 Version & Regression Information

Happens from 3.3.3 (Removing NoInfer) up to the nightly version.

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAjgHgEoFEDKBGAPmQG8BYAKGSuRgFcQE8AKASgC5t8BuC6m+hACYA-GxAQAbtB6VqdBgGYmAEzhg4bAHIB7AJIgY0TLkLsJ24MpkBfCqEixEKAEbH8AGmSdBRMrKryjKwcJjJ8gYIqaho6+oZQbqYhOIJhcgJKqupaegZG3gRmFlYUtuRgAJ4ADigAtsAAHomeBcgAvKiJRAA+yK6ceC24PjIUgWDA2iA0zV7DBFHZ9U0DQymFJBSlY0x+4QLMLJv+fMhQEGC0UNN4ggq81NbuDwECkVlwR3unVOeX18gAES3BSAl7IJ7gwKZaJfZAAenhyAACnAoHBahdoMgAOQfHHIYC1KoAG2ACGAYBJFWQAAs4ABnVDTHFwEAVAmVGoAOi2JzK1hYXCAA

💻 Code

interface a<RES1> {
    func1():RES1;
    func2?:never;
    func3(data:NoInfer<RES1>):void;
}
interface b<RES1, RES2> {
    func1():RES1;
    func2(data:NoInfer<RES1>):RES2;
    func3(data:NoInfer<RES2>):void;
}
type mix<RES1, RES2> = a<RES1> | b<RES1, RES2>;

function f<RES1, RES2>(data:mix<RES1, RES2>) {

}
f({
    func1() {
        return 123
    },
    func2(data) {
        return "123"
    },
    func3(data) { // Parameter 'data' implicitly has an 'any' type.

    }
});

🙁 Actual behavior

It happens that func3 argument is not properly inferred as is trying to infer the a interface, although it doesn't apply because the requirement for that interface is to explicitly not have a func2 property.

If I change both func3 arguments to the same type (RES1 for example) it works, so it is actually getting both interfaces.

🙂 Expected behavior

I guess that it should first get the applicable interfaces, and then try to infer the values depending on the applicable interfaces only.

Additional information about the issue

Still, I'm not sure about this approach. What I want is to have a func3 method that receives the result of func1 or func2 depending if func2 exists or not, like a transformer, but everything inside the same interface.

@Llorx Llorx changed the title Apply inference in method interfaces when using a discriminator Apply inference in method arguments when using a discriminator Oct 29, 2024
@jcalz
Copy link
Contributor

jcalz commented Oct 29, 2024

Kind of looks like another one for the #47599 pile.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Nov 1, 2024
@RyanCavanaugh
Copy link
Member

Yeah, this would require an unbounded number of passes (or maybe one pass per type argument, but it's unclear which order you'd use)

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Design Limitation" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants