Skip to content

Can't use keyof index for indexing intersection of Partial<T> and another type in generic class<T>. #22286

Closed

Description

@Griffork

Bug
I'm trying to use an object's key to index an intersection into a partial of an object and another object, but I'm getting an error.
Note: this error only shows up in a generic class. Using plain types I was not able to replicate it.

TypeScript Version:
2.7.1-insiders.20180127 (playground)
2.3.1 (visual studio tools)
2.7.2 (command line)

Search Terms:
Keyof, intersection, union, Partial, index, merge.

Code

    interface Rum {
        "ho hum": string; 
    }

    interface Color {
        "blue": number;
    }

    class Thingy<T> {
        mything: Rum & Partial<T>;
        log(param: keyof T) {
            console.log(this.mything[param]); //ERROR!
        }
    }

Expected behavior:
No errors.

Actual behavior:
test.ts(13,25): error TS2536: Type 'keyof T' cannot be used to index type 'Rum & Partial'.

Playground link

Activity

  1. mhegazy commented on Mar 2, 2018

    @mhegazy
    Contributor

    Should be fixed by #22300

  2. Griffork commented on Mar 3, 2018

    @Griffork
    Author

    Mohamed Hegazy (@mhegazy) does this mean that the best typing I'm going to get is this.mything[param]: string | number?

  3. mhegazy commented on Mar 3, 2018

    @mhegazy
    Contributor

    it is actually (Rum & Partial<T>)[keyof T], which is generic, and not assignable to string | number, since T can have additional properties when it is instantiated.

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