Skip to content
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

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

Closed
Griffork opened this issue Mar 2, 2018 · 3 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@Griffork
Copy link

Griffork commented Mar 2, 2018

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

@mhegazy
Copy link
Contributor

mhegazy commented Mar 2, 2018

Should be fixed by #22300

@mhegazy mhegazy added the Bug A bug in TypeScript label Mar 2, 2018
@mhegazy mhegazy added this to the TypeScript 2.8 milestone Mar 2, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Mar 2, 2018
@Griffork
Copy link
Author

Griffork commented Mar 3, 2018

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

@mhegazy
Copy link
Contributor

mhegazy commented Mar 3, 2018

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.

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

3 participants