Skip to content

Commit 107dfc2

Browse files
committed
Fixed #103
1 parent 5505a09 commit 107dfc2

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

.changeset/empty-elephants-tell.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"@total-typescript/ts-reset": minor
3+
---
4+
5+
Changed the array.includes on readonly arrays to NOT be a type predicate. Before this change, this perfectly valid code would not behave correctly.
6+
7+
```ts
8+
type Code = 0 | 1 | 2;
9+
type SpecificCode = 0 | 1;
10+
11+
const currentCode: Code = 0;
12+
13+
// Create an empty list of subset type
14+
const specificCodeList: ReadonlyArray<SpecificCode> = [];
15+
16+
// This will be false, since 0 is not in []
17+
if (specificCodeList.includes(currentCode)) {
18+
currentCode; // -> SpecificCode
19+
} else {
20+
// This branch will be entered, and ts will think z is 2, when it is actually 0
21+
currentCode; // -> 2
22+
}
23+
```
24+
25+
Removing the type predicate brings ts-reset closer towards correctness.

readme.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,6 @@ users.includes("bryan");
188188

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

191-
It also makes `.includes` a type predicate, meaning you can use it to narrow wider types to a set enum:
192-
193-
```ts
194-
import "@total-typescript/ts-reset/array-includes";
195-
196-
const users = ["matt", "sofia", "waqas"] as const;
197-
198-
const isUser = (input: string) => {
199-
if (users.includes(input)) {
200-
// input is narrowed to "matt" | "sofia" | "waqas"
201-
console.log(input);
202-
}
203-
};
204-
```
205-
206191
### Make `Set.has()` less strict
207192

208193
```ts

src/entrypoints/array-includes.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface ReadonlyArray<T> {
44
includes(
55
searchElement: T | (TSReset.WidenLiteral<T> & {}),
66
fromIndex?: number,
7-
): searchElement is T;
7+
): boolean;
88
}
99

1010
interface Array<T> {

src/tests/array-includes.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ doNotExecute(async () => {
5050
arr.includes({ a: 1 });
5151
});
5252

53-
doNotExecute(async () => {
54-
let arr = [1, 2, 3] as const;
55-
56-
let member = 1;
57-
if (arr.includes(member)) {
58-
type tests = [Expect<Equal<typeof member, 1 | 2 | 3>>];
59-
}
60-
});
61-
6253
doNotExecute(async () => {
6354
const arr: Array<"1" | "2" | "3"> = ["1", "2", "3"];
6455

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy