Write compact(values): remove the falsy values, and type the result without the falsy types.
compact([1, 0, 2, null]) returns [1, 2], typed number[] rather than (number | null)[]. Keep every element type that can't be falsy, literal types included: compact([0, 1] as const) is typed 1[].
type Falsy = false | 0 | "" | null | undefined | 0n; function compact<T>(values: readonly T[]) { // Return the values that aren't falsy, typed without the falsy types.} // compact([1, 0, 2, null]); // [1, 2], typed number[]// compact(["a", "", undefined]); // ["a"], typed string[]The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…