Skip to content

Inferred return type of generic function with specialized arguments is unknown #40111

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
kol-93 opened this issue Aug 18, 2020 · 4 comments
Closed
Assignees
Labels
Duplicate An existing issue was already created

Comments

@kol-93
Copy link

kol-93 commented Aug 18, 2020

TypeScript Version: from 3.5.1 to 4.0.0-beta

Search Terms:
is:issue is:open return type generic unknown

Code

declare function f<T>(x: T): T;
declare function g(x: number): number;

type F = typeof f; // type F = <T>(x: T) => T;
type G = typeof g; // type G = (x: number) => number;

type TypedReturn<Fn extends (...args: any[]) => any, Args extends any[]> = Fn extends <A extends Args>(...args: A) => infer R ? R : never;
type TypedReturn1<Fn extends (arg: any) => any, Arg> = Fn extends <A extends Arg>(arg: A) => infer R ? R : never;

type FR = TypedReturn<F, [number]>;
// expected: type FR = number;
// actual:   type FR = unknown;

type GR = TypedReturn<G, [number]>;
// expected: type GR = number;
// actual:   type GR = number;

type FR1 = TypedReturn1<F, number>;
// expected: type FR1 = number;
// actual:   type FR1 = unknown;

type GR1 = TypedReturn1<G, number>;
// expected: type GR1 = number;
// actual:   type GR1 = number;

// These types are proofs of a bug
type F_Infer_OK = F extends (...args: [number]) => number ? true : false;
// type F_Infer_OK = true;
type F_Infer1_OK = F extends (x: number) => number ? true : false;
// type F_Infer1_OK = true;

type F_Infer_FAIL_1 = F extends (...args: [number]) => string ? true : false;
// type F_Infer_FAIL_1 = false;
type F_Infer1_FAIL_1 = F extends (x: number) => string ? true : false;
// type F_Infer1_FAIL_1 = false;

type F_Infer_FAIL_2 = F extends (...args: [string]) => number ? true : false;
// type F_Infer_FAIL_2 = false;
type F_Infer1_FAIL_2 = F extends (x: string) => number ? true : false;
// type F_Infer1_FAIL_2 = false;

Expected behavior:
See comments in Code section

Actual behavior:
See comments in Code section

Playground link

Related Issues:
No direct related issues found.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Aug 19, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.1.0 milestone Aug 19, 2020
@awerlogus
Copy link

Related to #38182

@RyanCavanaugh RyanCavanaugh added Rescheduled This issue was previously scheduled to an earlier milestone and removed Rescheduled This issue was previously scheduled to an earlier milestone labels Aug 31, 2020
@ahejlsberg
Copy link
Member

This is effectively a duplicate of #22617. As I mention there, the fix would be to have type inference in conditional types perform instantiation of a source type in the context of a target type when the source type is a generic function type.

@ahejlsberg ahejlsberg added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels Oct 21, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

1 similar comment
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants