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)[];
I'm getting false-positive oxclint errors because the types of
intersectionassume 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
intersectionis for validation or filtering an array of strings/numbers against a reference array of valid values. Example:Here's the proposed change. Happy to PR this if that would be welcome.
Current:
Suggested: