-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added AsNumericArray<T> utility
- Loading branch information
1 parent
76f77bd
commit 9c86e29
Showing
3 changed files
with
53 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AllLiteral } from "../boolean-logic"; | ||
import { RemoveNever } from "../containers"; | ||
import { NumberLike } from "../numeric-literals"; | ||
import { AsNumber } from "../type-conversion"; | ||
|
||
|
||
/** | ||
* **AsNumericArray**`<T>` | ||
* | ||
* Converts into a numeric array: | ||
* | ||
* - `number` values are proxied through | ||
* - `${number}` values are converted to a number | ||
* - all other types are converted to `never` and removed | ||
*/ | ||
export type AsNumericArray<T> = T extends readonly unknown[] | ||
? RemoveNever<{ | ||
[K in keyof T]: T[K] extends NumberLike | ||
? AsNumber<T[K]> | ||
: never | ||
}> | ||
: never; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Equal, Expect } from "@type-challenges/utils"; | ||
import { AsNumericArray } from "src/types/index"; | ||
import { describe, it } from "vitest"; | ||
|
||
// Note: while type tests clearly fail visible inspection, they pass from Vitest | ||
// standpoint so always be sure to run `tsc --noEmit` over your test files to | ||
// gain validation that no new type vulnerabilities have cropped up. | ||
|
||
describe("AsNumericArray<T>", () => { | ||
|
||
it("happy path", () => { | ||
type NoChange = AsNumericArray<[1,2,3]>; | ||
type Mixed = AsNumericArray<[1,2,`3`]>; | ||
type StrLit = AsNumericArray<[`1`,`2`,`3`]>; | ||
|
||
type IgnoreInvalid = AsNumericArray<[1,2,null,false,3]>; | ||
|
||
// @ts-ignore | ||
type cases = [ | ||
Expect<Equal<NoChange, [1,2,3]>>, | ||
Expect<Equal<Mixed, [1,2,3]>>, | ||
Expect<Equal<StrLit, [1,2,3]>>, | ||
|
||
Expect<Equal<IgnoreInvalid, [1,2,3]>> | ||
]; | ||
}); | ||
|
||
}); |