Skip to content

Commit

Permalink
Added support for indexOf #91
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Mar 8, 2023
1 parent 4765413 commit ce9db42
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-hotels-remember.md
@@ -0,0 +1,5 @@
---
"@total-typescript/ts-reset": minor
---

Added support for widening in `Array.lastIndexOf`, `Array.indexOf`, `ReadonlyArray.lastIndexOf` and `ReadonlyArray.indexOf`.
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -67,6 +67,11 @@
"types": "./dist/utils.d.ts",
"import": "./dist/utils.mjs",
"default": "./dist/utils.js"
},
"./array-index-of": {
"types": "./dist/array-index-of.d.ts",
"import": "./dist/array-index-of.mjs",
"default": "./dist/array-index-of.js"
}
},
"keywords": [],
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Expand Up @@ -188,6 +188,14 @@ users.includes("bryan");

This means you can test non-members of the array safely.

### Make `.indexOf` on `as const` arrays less strict

```ts
import "@total-typescript/ts-reset/array-index-of";
```

Exactly the same behaviour of `.includes` (explained above), but for `.lastIndexOf` and `.indexOf`.

### Make `Set.has()` less strict

```ts
Expand Down
23 changes: 23 additions & 0 deletions src/entrypoints/array-index-of.d.ts
@@ -0,0 +1,23 @@
/// <reference path="utils.d.ts" />

interface ReadonlyArray<T> {
lastIndexOf(
searchElement: T | (TSReset.WidenLiteral<T> & {}),
fromIndex?: number,
): number;
indexOf(
searchElement: T | (TSReset.WidenLiteral<T> & {}),
fromIndex?: number,
): number;
}

interface Array<T> {
lastIndexOf(
searchElement: T | (TSReset.WidenLiteral<T> & {}),
fromIndex?: number,
): number;
indexOf(
searchElement: T | (TSReset.WidenLiteral<T> & {}),
fromIndex?: number,
): number;
}
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Expand Up @@ -5,3 +5,4 @@
/// <reference path="array-includes.d.ts" />
/// <reference path="set-has.d.ts" />
/// <reference path="map-has.d.ts" />
/// <reference path="array-index-of.d.ts" />
133 changes: 133 additions & 0 deletions src/tests/array-index-of.ts
@@ -0,0 +1,133 @@
import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(async () => {
const arr = ["1", "2", "3"] as const;

// Look ma, no error!
arr.indexOf("4");

// Look ma, proper errors!
arr.indexOf(
// @ts-expect-error
2,
);
arr.indexOf(
// @ts-expect-error
true,
);
});

doNotExecute(async () => {
const arr = [1, 2, 3] as const;

arr.indexOf(4);
arr.indexOf(
// @ts-expect-error
true,
);
arr.indexOf(
// @ts-expect-error
"str",
);
});

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

arr.indexOf(
// @ts-expect-error
4,
);

arr.indexOf({ a: 1 });
});

doNotExecute(async () => {
const arr: Array<"1" | "2" | "3"> = ["1", "2", "3"];

arr.indexOf("4");

arr.indexOf(
// @ts-expect-error
2,
);
arr.indexOf(
// @ts-expect-error
true,
);
});

// lastIndexOf

doNotExecute(async () => {
const arr = ["1", "2", "3"] as const;

// Look ma, no error!
arr.lastIndexOf("4");

// Look ma, proper errors!
arr.lastIndexOf(
// @ts-expect-error
2,
);
arr.lastIndexOf(
// @ts-expect-error
true,
);
});

doNotExecute(async () => {
const arr = [1, 2, 3] as const;

arr.lastIndexOf(4);
arr.lastIndexOf(
// @ts-expect-error
true,
);
arr.lastIndexOf(
// @ts-expect-error
"str",
);
});

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

arr.lastIndexOf(
// @ts-expect-error
4,
);

arr.lastIndexOf({ a: 1 });
});

doNotExecute(async () => {
const arr: Array<"1" | "2" | "3"> = ["1", "2", "3"];

arr.lastIndexOf("4");

arr.lastIndexOf(
// @ts-expect-error
2,
);
arr.lastIndexOf(
// @ts-expect-error
true,
);
});

0 comments on commit ce9db42

Please sign in to comment.