diff --git a/package.json b/package.json index bf4035f..9d6e074 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,11 @@ "types": "./dist/array-index-of.d.ts", "import": "./dist/array-index-of.mjs", "default": "./dist/array-index-of.js" + }, + "./array-fill": { + "types": "./dist/array-fill.d.ts", + "import": "./dist/array-fill.mjs", + "default": "./dist/array-fill.js" } }, "keywords": [], diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca830e6..2155f07 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2133,4 +2133,4 @@ packages: /yocto-queue/0.1.0: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} - dev: true + dev: true \ No newline at end of file diff --git a/src/entrypoints/array-fill.d.ts b/src/entrypoints/array-fill.d.ts new file mode 100644 index 0000000..590c62a --- /dev/null +++ b/src/entrypoints/array-fill.d.ts @@ -0,0 +1,28 @@ +/// + +// TODO: See how to remove the fill method from the interface completely +interface ReadonlyArray { + fill: never; +} + +// TODO: See how to override the fill method so that an Array.fill makes the Array an Array + +interface Array { + fill( + value: U | (TSReset.WidenLiteral & {}), + start?: undefined, + end?: undefined, + ): (U | (TSReset.WidenLiteral & {}))[]; + + fill( + value: U | (TSReset.WidenLiteral & {}), + start: 0, + end?: undefined, + ): (U | (TSReset.WidenLiteral & {}))[]; + + fill( + value: U | (TSReset.WidenLiteral & {}), + start: number, + end?: number, + ): (T | (U | (TSReset.WidenLiteral & {})))[]; +} diff --git a/src/entrypoints/recommended.d.ts b/src/entrypoints/recommended.d.ts index 7e6b2b4..aa9a98a 100644 --- a/src/entrypoints/recommended.d.ts +++ b/src/entrypoints/recommended.d.ts @@ -6,3 +6,4 @@ /// /// /// +/// diff --git a/src/tests/array-fill.ts b/src/tests/array-fill.ts new file mode 100644 index 0000000..ae201b2 --- /dev/null +++ b/src/tests/array-fill.ts @@ -0,0 +1,67 @@ +import { doNotExecute, Equal, Expect } from "./utils"; + +// TODO: Add tests that check the type overrides for the fill method once they are implemented + +doNotExecute(() => { + const arr = [] as const; + + // @ts-expect-error + arr.fill(2); +}); + +doNotExecute(() => { + const arr = [1, "2", { a: 3 }, []]; + + const newArr = arr.fill(2); + + // @ts-expect-error + type Test = Expect>; +}); + +doNotExecute(() => { + const arr = [1, "2", { a: 3 }, []]; + + const newArr = arr.fill(2); + + type Test = Expect>; +}); + +doNotExecute(() => { + const arr = [1, "2", { a: 3 }, []]; + + const newArr = arr.fill({ b: 1 } as const); + + type Test = Expect< + Equal< + typeof newArr, + { + readonly b: 1; + }[] + > + >; +}); + +doNotExecute(() => { + const arr = [1, "2", { a: 3 }, []]; + + const newArr = arr.fill(2, 0); + + // @ts-expect-error + type Test = Expect>; +}); + +doNotExecute(() => { + const arr = [1, "2", { a: 3 }, []]; + + const newArr = arr.fill(2, 0); + + type Test = Expect>; +}); + +doNotExecute(() => { + const arr = ["2", { a: 3 }, []]; + + const newArr = arr.fill(2, 1); + + type Test = Expect>; +});