Skip to content

Instanceof left-hand-side check rejects generic index access types #19298

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

Closed
mhegazy opened this issue Oct 18, 2017 · 2 comments · Fixed by #17912
Closed

Instanceof left-hand-side check rejects generic index access types #19298

mhegazy opened this issue Oct 18, 2017 · 2 comments · Fixed by #17912
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@mhegazy
Copy link
Contributor

mhegazy commented Oct 18, 2017

Reported by @yangchristian in #2775 (comment)

I repro @larsenwork's issue with the following:

type CreatedOrUpdatedAt =
    { createdAt: Date | string } |
    { updatedAt: Date | string };

const datePropComp = <T extends CreatedOrUpdatedAt, K extends keyof T>(a: T, b: T, field: K) => {
    // The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
    const dateA = a[field] instanceof Date ? a[field] : new Date(a[field]);
    const dateB = b[field] instanceof Date ? b[field] : new Date(b[field]);

    return dateA.getTime() - dateB.getTime();
};

datePropComp({ updatedAt: Date() }, { updatedAt: Date() }, 'updatedAt' )

Playground link

@ryanlin1986
Copy link

This blocks us from using typescript 2.5, please fix it so we can upgrade.

@sandersn
Copy link
Member

sandersn commented Dec 4, 2017

Duplicate of #17069 #17166 #15371

Fixed by #17912

@mhegazy mhegazy modified the milestones: TypeScript 2.7, TypeScript 2.8 Jan 9, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 10, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
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

Successfully merging a pull request may close this issue.

3 participants