Skip to content

intersection should return an array of the intersection of the types of each array's elements #147

Description

@justingrant

I'm getting false-positive oxclint errors because the types of intersection assume that both its arguments and also its return value are all the same type. Instead, the return value should be the intersection of the types of its arguments' array elements.

This is particularly relevant for literal types, because a common use of intersection is for validation or filtering an array of strings/numbers against a reference array of valid values. Example:

const actualParamNames = ['a', 'b', 'c', 'd'];
const validNames = ['a', 'b'] as const;
const validParams = intersection(actualParamNames, validNames);
// expected: validParams: ("a" | "b")[]
// actual: validParams: string

Here's the proposed change. Happy to PR this if that would be welcome.

Current:

export function intersection<T>(list1: readonly T[]): (list2: readonly T[]) => T[];
export function intersection<T>(list1: readonly T[], list2: readonly T[]): T[];

Suggested:

export function intersection<T, U>(list1: readonly T[]): (list2: readonly U[]) => (T & U)[];
export function intersection<T, U>(list1: readonly T[], list2: readonly U[]): (T & U)[];

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions