Skip to content

Generic inference from return type #39688

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

Closed
idoros opened this issue Jul 21, 2020 · 3 comments
Closed

Generic inference from return type #39688

idoros opened this issue Jul 21, 2020 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@idoros
Copy link

idoros commented Jul 21, 2020

TypeScript Version: 4.0.0-dev.20200715

Search Terms: generic inference

Code

interface Test<C> {
    title: string;
    test(context: C): unknown;
}
declare function suite<C>(setup: () => C, ...items: Test<C>[]): Test<C>;
declare function describe<PC, C extends PC>(setup: (context: PC) => C, ...items: Test<C>[]): Test<PC>;
declare function it<C>(title: string, test: (context: C) => unknown): Test<C>;

suite(
    () => ({
        a: 'param',
        b: 5
    }),
    it(``, ({a, b}) => {a.charAt(b)}), // <- generic works for first level
    it(``, () => {}),
    describe(
        ({a, b}) => ({a, b, c: true}),
        // it(``, ({a, c}) => {}) // <- uncomment to override generic ({a: any, c: any})
    )
);

Expected behavior:

Inference should work in the same way for describe as it works for suite.

Actual behavior:

Placing the it under the 2nd level triggers the inference in a different way. The difference between them is that in the describe signature the PC generic from the first level is used in the setup function - removing the context: PC from the setup function or just not using it in the actual setup will make both levels work the same.

Playground Link: Playground Link

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jul 21, 2020
@RyanCavanaugh
Copy link
Member

Looks like a job for #30134

@idoros
Copy link
Author

idoros commented Jun 2, 2021

@RyanCavanaugh , this still doesn't work in the latest/nightly version, is this something that will not get resolved, or just a duplicate?

@RyanCavanaugh
Copy link
Member

@idoros it's unlikely this will be changed barring a full redesign of generic inference

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

2 participants