Skip to content

Issues with 'keyof' in 'for-in' statements and 'in' operator #12513

Closed

Description

TypeScript Version: nightly

Code

function f1<K extends string, T>(obj: { [P in K]: T }, k: K) {
    const b = k in obj;    // Error
    let k1: K;
    for (k1 in obj) {      // Error
        let x1 = obj[k1];  // Error
    }
    for (let k2 in obj) {
        let x2 = obj[k2];  // Error
    }
}

function f2<T>(obj: { [P in keyof T]: T[P] }, k: keyof T) {
    const b = k in obj;
    let k1: keyof T;
    for (k1 in obj) {
        let x1 = obj[k1];
    }
    for (let k2 in obj) {
        let x2 = obj[k2];
    }
}

function f3<T, K extends keyof T>(obj: { [P in K]: T[P] }, k: K) {
    const b = k in obj;    // Error
    let k1: K;
    for (k1 in obj) {      // Error
        let x1 = obj[k1];
    }
    for (let k2 in obj) {
        let x2 = obj[k2];  // Error
    }
}

Expected behavior:

No errors.

Actual behavior:

Multiple errors as indicated in example.

Activity

  1. added this to the milestone on Nov 26, 2016
  2. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions