Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typesafe array fill #159

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -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": [],
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/entrypoints/array-fill.d.ts
@@ -0,0 +1,28 @@
/// <reference path="utils.d.ts" />

// TODO: See how to remove the fill method from the interface completely
interface ReadonlyArray<T> {
fill: never;
}

// TODO: See how to override the fill method so that an Array<T>.fill<U> makes the Array<T> an Array<U>

interface Array<T> {
fill<U>(
value: U | (TSReset.WidenLiteral<U> & {}),
start?: undefined,
end?: undefined,
): (U | (TSReset.WidenLiteral<U> & {}))[];

fill<U>(
value: U | (TSReset.WidenLiteral<U> & {}),
start: 0,
end?: undefined,
): (U | (TSReset.WidenLiteral<U> & {}))[];

fill<U>(
value: U | (TSReset.WidenLiteral<U> & {}),
start: number,
end?: number,
): (T | (U | (TSReset.WidenLiteral<U> & {})))[];
}
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Expand Up @@ -6,3 +6,4 @@
/// <reference path="set-has.d.ts" />
/// <reference path="map-has.d.ts" />
/// <reference path="array-index-of.d.ts" />
/// <reference path="array-fill.d.ts" />
67 changes: 67 additions & 0 deletions 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<Equal<typeof newArr, typeof arr>>;
});

doNotExecute(() => {
const arr = [1, "2", { a: 3 }, []];

const newArr = arr.fill(2);

type Test = Expect<Equal<typeof newArr, number[]>>;
});

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<Equal<typeof newArr, typeof arr>>;
});

doNotExecute(() => {
const arr = [1, "2", { a: 3 }, []];

const newArr = arr.fill(2, 0);

type Test = Expect<Equal<typeof newArr, number[]>>;
});

doNotExecute(() => {
const arr = ["2", { a: 3 }, []];

const newArr = arr.fill(2, 1);

type Test = Expect<Equal<typeof newArr, (number | (typeof arr)[number])[]>>;
});