Skip to content

.filter() can't handle union of arrays #168

Open
@mattiaz9

Description

@mattiaz9

Example:

type ClientA = { firstName: string; lastName: string; isAdmin: boolean }
type ClientB = { fullName: string; isAdmin: boolean }
type ClientList = ClientA[] | ClientB[]

const list: ClientList = []
const admins = list.filter(c => c.isAdmin)
//                         ^^^^^^^^^^^^^^
// Error: Argument of type '<T>(c: T | undefined) => any' is not assignable to parameter of type 'BooleanConstructor'. 
// Type '<T>(c: T | undefined) => any' provides no match for the signature 'new (value?: any): Boolean'

In this example both ClientA and ClientB has a common field isAdmin but .filter() can't handle this union.

Removing ts-reset fixed the error.
Also changing the type to (ClientA | ClientB)[] fixed the error, but it can be inconvenient in some cases because of a forced type cast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions