Skip to content

Higher order inference for lookup types does not work #20124

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
weswigham opened this issue Nov 18, 2017 · 0 comments
Closed

Higher order inference for lookup types does not work #20124

weswigham opened this issue Nov 18, 2017 · 0 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@weswigham
Copy link
Member

weswigham commented Nov 18, 2017

Inference for T[K] to U[Y] was supposedly introduced in #18391, however looking over the code, this case in inferTypes in checker.ts:

                else if (source.flags & TypeFlags.IndexedAccess && target.flags & TypeFlags.IndexedAccess) {
                    inferFromTypes((<IndexedAccessType>source).objectType, (<IndexedAccessType>target).objectType);
                    inferFromTypes((<IndexedAccessType>source).indexType, (<IndexedAccessType>target).indexType);
                }

is unreachable code.
This is because the first if statement in the if-else if chain is

if (target.flags & TypeFlags.TypeVariable) {

which is true when the target is TypeFlags.IndexedAccess, as TypeFlags.TypeVariable = TypeFlags.TypeParameter | TypeFlags.IndexedAccess.

TypeScript Version: 2.6.1-insiders.20171019 (current playground)

Code

declare function get<U, Y extends keyof U>(x: U, y: Y): U[Y];
declare function find<T, K extends keyof T>(o: T[K]): [T, K];

function impl<A, B extends keyof A>(a: A, b: B) {
    const item = get(a, b);
    return find(item);
}

Expected behavior:
impl has return type [A, B]

Actual behavior:
impl has return type [{}, never]

@weswigham weswigham added the Bug A bug in TypeScript label Nov 18, 2017
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Nov 20, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 11, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants