Skip to content

Type error when using generic function result as argument, but not when assigned to temp variable first #35339

Closed
@fatcerberus

Description

@fatcerberus

Issue originally discovered by @eyelidlessness on Gitter.

TypeScript Version: 3.8.0-dev.20191123

Search Terms: generic returntype

Code

declare const foo: <
    R1,
    F1 extends () => R1,
    R2,
    F2 extends (input: ReturnType<F1>) => R2
>(
    f1: F1,
    f2: F2
) => any

const f1 = () => ({ a: 'a' })
const f2 = (input: { a: string }) => ({ b: 'b' })

foo(f1, f2) // Good

const makeF1 = () => f1;

foo(makeF1(), f2) // Good

const makeF1p = <T>(t: T) => f1;

foo(
    makeF1p(''),
    f2 // Error WHY :(
);

const made = makeF1p('');

foo(
    made, // This is fine
    f2
)

Expected behavior:

Both foo() calls should compile as the only difference is whether the result of a function call is assigned to a temporary first.

Actual behavior:

The first call to foo(), where makeF1p() is called inline, produces a type error at compile time. The second, where the return value of makeF1p() is assigned to temp variable first, compiles fine. The error is:

Argument of type '(input: { a: string; }) => { b: string; }' is not assignable to parameter of type '(input: unknown) => unknown'.
  Types of parameters 'input' and 'input' are incompatible.
    Type 'unknown' is not assignable to type '{ a: string; }'.

It almost seems like the type parameter inference machinery is leaking outside the scope of the call expression or something? It's weird, at any rate.

Playground Link:

https://www.typescriptlang.org/play/?ts=3.8.0-dev.20191123&ssl=1&ssc=1&pln=33&pc=1#code/CYUwxgNghgTiAEYD2A7AzgF3gMyUgXPADwBQ858ASgIwA0ZFAYtfCAB4YgrBrwAUASngBeAHxU6DcpQBM9CvEYzWHLj34BLFAAcArhkKUQGXTBQAVAJ7aQRZqKFiqMkqL5Sc1Qs3kVsM7xdHcSgUSxISZHQsbBZhfmD+AG94KEIAcih0+ABfAUjUTBxleL4tPQN4FLT4TBgtAHNcxL4UgCMMtuy8iNwkPljaYqEAehH4AHE8YAioooBbKABrEGYRBJFxWIBuXrw+RZXmQSH-UfGppBmC6PhD1eptdaJzN0rzRJ29-o975m0+Ol0gJfOR-PAxvAAKIwGBIGDwADqAAkAJrwfDuAS7G4LKCgdZ-R6A4E4vruBSLUBDSHmAAWGl4jJwWhAHn8JHyQA

Related Issues: I didn't find any, but @jack-williams says #30215 might be the root cause.

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