diff --git a/package.json b/package.json index 09f4bc1..d15c4a9 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,11 @@ "types": "./dist/storage.d.ts", "import": "./dist/storage.mjs", "default": "./dist/storage.js" + }, + "./every-boolean": { + "types": "./dist/every-boolean.d.ts", + "import": "./dist/every-boolean.mjs", + "default": "./dist/every-boolean.js" } }, "keywords": [], diff --git a/src/entrypoints/every-boolean.d.ts b/src/entrypoints/every-boolean.d.ts new file mode 100644 index 0000000..53fdd4b --- /dev/null +++ b/src/entrypoints/every-boolean.d.ts @@ -0,0 +1,23 @@ +/// + +interface Array { + every( + predicate: BooleanConstructor, + thisArg?: any, + ): this is this extends S[] + ? TSReset.NonFalsy[] + : this[number] extends TSReset.NonFalsy + ? this + : never; +} + +interface ReadonlyArray { + every( + predicate: BooleanConstructor, + thisArg?: any, + ): this is this extends S[] + ? TSReset.NonFalsy[] + : this[number] extends TSReset.NonFalsy + ? this + : never; +} diff --git a/src/tests/every-boolean.ts b/src/tests/every-boolean.ts new file mode 100644 index 0000000..bbb454a --- /dev/null +++ b/src/tests/every-boolean.ts @@ -0,0 +1,72 @@ +import { doNotExecute, Equal, Expect } from "./utils"; + +doNotExecute(() => { + const arr = [1, 2, 3, undefined]; + + if (arr.every(Boolean)) { + type tests = [Expect>]; + } else { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const arr = [1, 2, 3, "four"]; + + if (arr.every(Boolean)) { + type tests = [Expect>]; + } else { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const arr = ["1", "2"] as const; + + if (arr.every(Boolean)) { + type tests = [Expect>]; + } else { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const arr = ["1", "2", undefined] as const; + + if (arr.every(Boolean)) { + type tests = [Expect>]; + } else { + type tests = [Expect>]; + } +}); + +doNotExecute(() => { + const arr = [0, null, undefined, false, ""] as const; + + if (arr.every(Boolean)) { + type tests = [Expect>]; + } else { + type tests = [ + Expect>, + ]; + } +}); + +doNotExecute(() => { + const arr: (0 | null | undefined | false | "" | 0n)[] = [ + 0, + null, + undefined, + false, + "", + 0n, + ]; + + if (arr.every(Boolean)) { + type tests = [Expect>]; + } else { + type tests = [ + Expect>, + ]; + } +});