What is the correct fix?
function pick<T>(list: T[], i: number): T {
return list[i];
}
const v = pick([1, 2], 9);
- Return
T | undefinedand guard the index - Return
T | neverand guard the index - Change the parameter type to
i: T - Mark the whole function as
async
An out-of-range index yields undefined, which the declared return type wrongly denies.
Play Problem Solving ▸