forked from total-typescript/ts-reset
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from DeepDoge/sheraff/array-at
[ReadonlyArray] `.at()` fix `undefined` issue
- Loading branch information
Showing
3 changed files
with
76 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
/// <reference path="utils.d.ts" /> | ||
|
||
interface ReadonlyArray<T> { | ||
at<I extends number> (index: I): `${I}` extends `-${infer J extends number}` | ||
? `${TSReset.Subtract<this["length"], J>}` extends `-${number}` | ||
? undefined | ||
: this[TSReset.Subtract<this["length"], J>] | ||
: this[I] | ||
} | ||
at< | ||
const N extends number, | ||
I extends number = `${N}` extends `${infer J extends number}.${number}` | ||
? J | ||
: N, | ||
>( | ||
index: N, | ||
): TSReset.Equal<I, number> extends true | ||
? T | undefined | ||
: `${I}` extends `-${infer J extends number}` | ||
? `${TSReset.Subtract<this["length"], J>}` extends `-${number}` | ||
? undefined | ||
: this[TSReset.Subtract<this["length"], J>] | ||
: this[I]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,60 @@ | ||
import { doNotExecute, Equal, Expect } from "./utils"; | ||
|
||
doNotExecute(async () => { | ||
const arr = [false, 1, '2'] as const; | ||
const arr = [false, 1, "2"] as const; | ||
|
||
const a = arr.at(0) | ||
const b = arr.at(1) | ||
const c = arr.at(2) | ||
const d = arr.at(3) | ||
const a = arr.at(0); | ||
const b = arr.at(1); | ||
const c = arr.at(2); | ||
const d = arr.at(3); | ||
const e = arr.at(1.5); | ||
type tests = [ | ||
Expect<Equal<typeof a, false>>, | ||
Expect<Equal<typeof b, 1>>, | ||
Expect<Equal<typeof c, '2'>>, | ||
Expect<Equal<typeof c, "2">>, | ||
Expect<Equal<typeof d, undefined>>, | ||
] | ||
Expect<Equal<typeof e, 1>>, | ||
]; | ||
}); | ||
|
||
doNotExecute(async () => { | ||
const arr = [false, 1, '2'] as const; | ||
const arr = [false, 1, "2"] as const; | ||
|
||
const a = arr.at(-1) | ||
const b = arr.at(-2) | ||
const c = arr.at(-3) | ||
const d = arr.at(-4) | ||
const a = arr.at(-1); | ||
const b = arr.at(-2); | ||
const c = arr.at(-3); | ||
const d = arr.at(-4); | ||
const e = arr.at(-1.5); | ||
type tests = [ | ||
Expect<Equal<typeof a, '2'>>, | ||
Expect<Equal<typeof a, "2">>, | ||
Expect<Equal<typeof b, 1>>, | ||
Expect<Equal<typeof c, false>>, | ||
Expect<Equal<typeof d, undefined>>, | ||
] | ||
Expect<Equal<typeof e, "2">>, | ||
]; | ||
}); | ||
|
||
doNotExecute(async () => { | ||
const arr = [false, 1, '2'] as const; | ||
const index: number = 1 | ||
const a = arr.at(index) | ||
// WARN: with `"strictNullChecks": true,` the correct type here should include `undefined` | ||
// but the current implementation does not type this as including `undefined` | ||
type tests = [ | ||
Expect<Equal<typeof a, false | 1 | "2">>, | ||
] | ||
}); | ||
const arr = [false, 1, "2"] as const; | ||
|
||
const index = 0 as 0 | 1 | ||
|
||
const a = arr.at(index); | ||
type tests = [Expect<Equal<typeof a, false | 1>>]; | ||
}); | ||
|
||
doNotExecute(async () => { | ||
const arr = [false, true, 1, "2"] as const; | ||
|
||
const index = -1 as -1 | -2 | ||
|
||
const a = arr.at(index); | ||
type tests = [Expect<Equal<typeof a, "2" | 1>>]; | ||
}); | ||
|
||
doNotExecute(async () => { | ||
const arr = [false, 1, "2"] as const; | ||
const index = 1 as number; | ||
const a = arr.at(index); | ||
type tests = [Expect<Equal<typeof a, false | 1 | "2" | undefined>>]; | ||
}); |