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

type not inferred correctly in generic function #32109

Closed
yaymukund opened this issue Jun 26, 2019 · 4 comments
Closed

type not inferred correctly in generic function #32109

yaymukund opened this issue Jun 26, 2019 · 4 comments
Labels
Fixed A PR has been merged for this issue

Comments

@yaymukund
Copy link

yaymukund commented Jun 26, 2019

TypeScript Version: 3.5.2

Search Terms: generic type constraint undefined enum

Code

enum MyEvent {
    Start = 'start',
    Stop = 'stop',
}

interface MyEventOptions {
    [MyEvent.Start]: { startTime: number },
    [MyEvent.Stop]: { endTime: number },
}

class EventService {
    private eventCounts: { [key in MyEvent]?: number };

    constructor() {
        // I don't want to have to list every possible enum member here
        this.eventCounts = {};
    }

    private startEvent() {
        this.report(MyEvent.Start, { startTime: 0 });

        // This should be invalid:
        // 
        // this.report(MyEvent.Start, { endTime: 0 });
    }

    private report<T extends keyof MyEventOptions>(event: T, options: MyEventOptions[T]) {
        // errors with `Type 'undefined' is not assignable to type 'number'.`
        const count: number = this.eventCounts[event] || 0;

        // This would do, for example:
        //
        // request(mixpanelUrl, { name: event, options });
    }
}

It doesn't complain if you change it to const count: number = (this.eventCounts.get(event) as number | undefined) || 0;

Expected behavior: Compiles without a problem.

Actual behavior:

Type '{ start?: number | undefined; stop?: number | undefined; }[T]' is not assignable to type 'number'.
  Type 'number | undefined' is not assignable to type 'number'.
    Type 'undefined' is not assignable to type 'number'.

Playground Link: https://is.gd/S57reZ

Related Issues: #32017

@sandersn sandersn added the Fixed A PR has been merged for this issue label Jun 26, 2019
@sandersn
Copy link
Member

@yaymukund typescript@next behaves the way you want it to, as far as I can tell. Can you confirm?

yaymukund pushed a commit to yaymukund/typescript-issue-32109 that referenced this issue Jun 27, 2019
@yaymukund
Copy link
Author

yaymukund commented Jun 27, 2019

@sandersn Thank you for taking a look.

It's not fixed even in typescript@next. It looks like it only errors when strictNullChecks is true. I've made a repository that reproduces the error.

@RyanCavanaugh
Copy link
Member

@yaymukund see #31908

@yaymukund
Copy link
Author

Thanks for the heads up— I'll keep an eye on that issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants