Skip to content

Commit

Permalink
includes() method update passed variable type
Browse files Browse the repository at this point in the history
  • Loading branch information
rawpixel-vincent committed Dec 31, 2024
1 parent 30ca21c commit 240d1ca
Show file tree
Hide file tree
Showing 9 changed files with 1,033 additions and 651 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ const bothWay = list.withPrefix('data.').withSuffix('.ext');
/** @type {any|unknown|'notInTheList'} */
let val;
list.includes(val); // No type error just boolean result.

if (list.includes(val)) {
// val type is now 'foo' | 'bar'
}

const valKnown = Math.random() > 0.5 ? 'notInTheList' : 'foo';
// valKnown type is 'foo' | 'notInTheList'
if (list.includes(valKnown)) {
// valKnown type is now 'foo'
} else if (val !== 'notInTheList') {
// valKnown type is now never
} else {
// valKnown type is 'notInTheList'
}

// similar types fix for indexOf, lastIndexOf, filter, some, every, findIndex and find methods.

// Get a copy of the underlying array -> T[]
Expand Down
1,630 changes: 1,008 additions & 622 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "string-literal-list",
"version": "1.31.0",
"version": "1.32.0",
"description": "an array for string literal",
"main": "stringList.cjs",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion strict.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion strict.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stringList.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stringList.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stringList.min.js

Large diffs are not rendered by default.

27 changes: 4 additions & 23 deletions types/list.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,6 @@ declare global {
: never
: never;

// withDerivatedSuffix<S extends string>(
// chars: S,
// ):
// | T
// | StringLiteralList.string.DropSuffix<
// StringLiteralList.string.DropSuffix<`${T}${S}${S}`, S>,
// `${S}${S}`
// > extends infer W extends string
// ? MaybeReadonly<Mut, IStringList<W, readonly W[], Mut, true>>
// : never;

// withDerivatedPrefix<S extends string>(
// chars: S,
// ):
// | T
// | StringLiteralList.string.DropPrefix<
// StringLiteralList.string.DropPrefix<`${S}${S}${T}`, S>,
// `${S}${S}`
// > extends infer W extends string
// ? MaybeReadonly<Mut, IStringList<W, readonly W[], Mut, true>>
// : never;

withReplace<S extends string, D extends string>(
searchValue: S,
replaceValue: D,
Expand Down Expand Up @@ -277,7 +255,10 @@ declare global {
at(n: number): Tuple[number] | undefined;

// Type override to prevent string not in type T issue
includes<PP = Tuple[number]>(val: PP, fromIndex?: number): boolean;
includes<PP = Tuple[number]>(
val: PP | string | null | undefined,
fromIndex?: number,
): val is Tuple[number];
indexOf<PP = Tuple[number]>(
searchElement: PP,
fromIndex?: number,
Expand Down

0 comments on commit 240d1ca

Please sign in to comment.