mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-18 07:29:16 -05:00
Make anyArray.filter(Boolean) return any[], not unknown[] (#31515)
* Add this-parameter workaround to Array.filter Allows anys.filter(Boolean) to once again return any[], not unknown[]. * Add any constraint to Boolean factory function I want to test how well this works. * Remove Boolean factory type guard * Remove typeGuardBoolean test
This commit is contained in:
committed by
GitHub
parent
1e7a77cf78
commit
b36c8a0690
24
tests/cases/compiler/booleanFilterAnyArray.ts
Normal file
24
tests/cases/compiler/booleanFilterAnyArray.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
interface Bullean { }
|
||||
interface BulleanConstructor {
|
||||
new(v1?: any): Bullean;
|
||||
<T>(v2?: T): v2 is T;
|
||||
}
|
||||
|
||||
interface Ari<T> {
|
||||
filter<S extends T>(cb1: (value: T) => value is S): T extends any ? Ari<any> : Ari<S>;
|
||||
filter(cb2: (value: T) => unknown): Ari<T>;
|
||||
}
|
||||
declare var Bullean: BulleanConstructor;
|
||||
declare let anys: Ari<any>;
|
||||
var xs: Ari<any>;
|
||||
var xs = anys.filter(Bullean)
|
||||
|
||||
declare let realanys: any[];
|
||||
var ys: any[];
|
||||
var ys = realanys.filter(Boolean)
|
||||
|
||||
var foo = [{ name: 'x' }]
|
||||
var foor: Array<{name: string}>
|
||||
var foor = foo.filter(x => x.name)
|
||||
var foos: Array<boolean>
|
||||
var foos = [true, true, false, null].filter((thing): thing is boolean => thing !== null)
|
||||
@@ -1,14 +0,0 @@
|
||||
// @strictNullChecks: true
|
||||
function test(strOrNull: string | null, strOrUndefined: string | undefined) {
|
||||
var str: string = "original";
|
||||
var nil: null;
|
||||
if (!Boolean(strOrNull)) {
|
||||
nil = strOrNull;
|
||||
}
|
||||
else {
|
||||
str = strOrNull;
|
||||
}
|
||||
if (Boolean(strOrUndefined)) {
|
||||
str = strOrUndefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user