* Handle type guard predicates on `Array<T>.find` If the `predicate` function passed to `Array<T>.find` or `ReadonlyArray<T>.find` is a type guard narrowing `value` to type `S`, then any returned element should also be narrowed to `S`. Adding test case and associated baselines * trailing whitespace after merge conflict
12 lines
586 B
TypeScript
12 lines
586 B
TypeScript
// @lib: es2015
|
|
|
|
// test fix for #18112, type guard predicates should narrow returned element
|
|
function isNumber(x: any): x is number {
|
|
return typeof x === "number";
|
|
}
|
|
|
|
const arrayOfStringsNumbersAndBooleans = ["string", false, 0, "strung", 1, true];
|
|
const foundNumber: number | undefined = arrayOfStringsNumbersAndBooleans.find(isNumber);
|
|
|
|
const readonlyArrayOfStringsNumbersAndBooleans = arrayOfStringsNumbersAndBooleans as ReadonlyArray<string | number | boolean>;
|
|
const readonlyFoundNumber: number | undefined = readonlyArrayOfStringsNumbersAndBooleans.find(isNumber);
|