面试题答案
一键面试function filterByType<T, U extends T>(arr: T[], typeGuard: (element: T) => element is U): U[] {
return arr.filter(typeGuard);
}
你可以这样使用:
function isString(s: any): s is string {
return typeof s ==='string';
}
const result = filterByType([1, 'a', 2, 'b'], isString);
console.log(result);