Skip to content

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

Description

Reported by Christian Yang (@yangchristian) in #2775 (comment)

I repro Andreas Larsen (@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

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