Skip to content

error TS2677: Type 'keyof E' is not assignable to type 'string' #23724

Closed
@AnyhowStep

Description

@AnyhowStep

TypeScript Version: Version 2.9.0-dev.20180426

Search Terms: keyof not assignable string

Code

//Just a minimal example, not what I am actually doing
function isKey<E> (str : string) : str is keyof E {
    return true;
}

Expected behavior:

Should compile fine. The body of the type guard may actually correctly assert that str is, indeed, keyof E

Works fine on TypeScript 2.8.3

Actual behavior:

error TS2677: A type predicate's type must be assignable to its parameter's type.
  Type 'keyof E' is not assignable to type 'string'.
    Type 'string | number | symbol' is not assignable to type 'string'.
      Type 'number' is not assignable to type 'string'

Playground Link: Works on the playground

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions