Skip to content

Bug from #23592 - Type 'keyof U & K' does not satisfy the constraint 'string | number | symbol' #23648

Description

TypeScript Version: 2.9.0-dev.20180424

Search Terms:
keyof, intersection, conditional types

Code

type Example<T, U> = { [K in keyof T]: K extends keyof U ? UnexpectedError<K> : NoErrorHere<K> }

type UnexpectedError<T extends PropertyKey> = T
type NoErrorHere<T extends PropertyKey> = T

Expected behavior:
No errors.

Actual behavior:
Error on K in UnexpectedError<K> with message

Type 'keyof U & K' does not satisfy the constraint 'string | number | symbol'.
  Type 'keyof U & K' is not assignable to type 'symbol'.

Playground Link:
Link (no error in playground)

Related Issues:
#23592

Activity

  1. ahejlsberg commented on Apr 24, 2018

    @ahejlsberg
    Member

    The issue here is that we don't lift constraints over intersection types. Here's a simpler repro:

    function foo<T extends string | number, U extends string | number>(x: T & U) {
        let y: string | number = x;  // Error, but shouldn't be
    }

    We need to lift the individual constraints of T and U such that the intersection type T & U appears to have the constraint string | number (formed by intersecting the individual constraints of T and U).

  2. ahejlsberg commented on Apr 24, 2018

    @ahejlsberg
    Member

    The reason this shows up with #23592 is that we now have union type constraints for keyof T.

  3. ahejlsberg commented on Apr 24, 2018

    @ahejlsberg
    Member

    Mohamed Hegazy (@mhegazy) Do you recall if we already have an issue for this?

  4. mhegazy commented on Apr 25, 2018

    @mhegazy
    Contributor

    Mohamed Hegazy (@mhegazy) Do you recall if we already have an issue for this?

    do not think i have seen this one before.

  5. locked and limited conversation to collaborators on Jul 31, 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