Skip to content

Generic inference from return type #39688

Closed
@idoros

Description

@idoros

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions