Skip to content

T[keyof T] type constraint lost in function body #12991

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
jaredru opened this issue Dec 16, 2016 · 2 comments
Closed

T[keyof T] type constraint lost in function body #12991

jaredru opened this issue Dec 16, 2016 · 2 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jaredru
Copy link

jaredru commented Dec 16, 2016

TypeScript Version: 2.1.4 / nightly (2.2.0-dev.20161216)

function fn<T extends {[P in K]: string}, K extends keyof T>(obj: T, key: K) {
  const val: string = obj[key];  // error TS2322: Type 'T[K]' is not assignable to type 'string'
}

fn({ str: "s", num: 1 }, "str"); // OK
fn({ str: "s", num: 1 }, "num"); // error TS2345: Argument of type '{ str: string; num: number; }' is not assignable to parameter of type '{ num: string; }

Expected behavior:

Given the constraints, I would expect obj[key] in this case to be of type string.

Actual behavior:

The type of obj[key] is the less specific T[K].

Considering the error at the second call site (which is expected), it seems that the type checking done there is complete and working. However, that same type info is not carried through into the function body itself.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 21, 2016
@ajafff
Copy link
Contributor

ajafff commented Jan 23, 2018

I witnessed the same bug. I noticed that this problem only exists if both obj and key are TypeParameters.

// typescript@2.7.0-rc with --strictNullChecks
declare function foo(p: string): void;

interface I {
    foo?: string;
    bar?: string;
}

function working1<T extends I>(o: T, k: keyof I) {
    foo(o[k]); // getting expected type error here, because type is `string | undefined`
}

function working2<K extends keyof I>(o: I, k: K) {
    foo(o[k]); // getting expected type error here, because type is `string | undefined`
}

function notWorking<T extends I, K extends keyof T>(o: T, k: K) {
    foo(o[k]); // expected type error, but got none
}

Inside notWorking o[k] is treated as any. At least that's what checker.getApparentType(checker.getTypeAtLocation(node)) tells me.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 1, 2018

These should be working now as expected.

@mhegazy mhegazy closed this as completed Feb 1, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Feb 1, 2018
@mhegazy mhegazy added this to the TypeScript 2.7.1 milestone Feb 1, 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

4 participants