@darthcav/ts-utils 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/type-testing/Assert.d.ts +23 -0
- package/dist/type-testing/Assert.d.ts.map +1 -0
- package/dist/type-testing/Assert.js +2 -0
- package/dist/type-testing/Assert.js.map +1 -0
- package/dist/type-testing/Equal.d.ts +25 -0
- package/dist/type-testing/Equal.d.ts.map +1 -0
- package/dist/type-testing/Equal.js +2 -0
- package/dist/type-testing/Equal.js.map +1 -0
- package/dist/type-testing/Extends.d.ts +22 -0
- package/dist/type-testing/Extends.d.ts.map +1 -0
- package/dist/type-testing/Extends.js +2 -0
- package/dist/type-testing/Extends.js.map +1 -0
- package/dist/type-testing/IsAny.d.ts +17 -0
- package/dist/type-testing/IsAny.d.ts.map +1 -0
- package/dist/type-testing/IsAny.js +2 -0
- package/dist/type-testing/IsAny.js.map +1 -0
- package/dist/type-testing/IsNever.d.ts +17 -0
- package/dist/type-testing/IsNever.d.ts.map +1 -0
- package/dist/type-testing/IsNever.js +2 -0
- package/dist/type-testing/IsNever.js.map +1 -0
- package/dist/type-testing/IsUnknown.d.ts +20 -0
- package/dist/type-testing/IsUnknown.d.ts.map +1 -0
- package/dist/type-testing/IsUnknown.js +2 -0
- package/dist/type-testing/IsUnknown.js.map +1 -0
- package/dist/type-testing/Not.d.ts +17 -0
- package/dist/type-testing/Not.d.ts.map +1 -0
- package/dist/type-testing/Not.js +2 -0
- package/dist/type-testing/Not.js.map +1 -0
- package/package.json +5 -7
- package/src/index.ts +14 -0
- package/src/type-testing/Assert.ts +22 -0
- package/src/type-testing/Equal.ts +27 -0
- package/src/type-testing/Extends.ts +21 -0
- package/src/type-testing/IsAny.ts +16 -0
- package/src/type-testing/IsNever.ts +16 -0
- package/src/type-testing/IsUnknown.ts +21 -0
- package/src/type-testing/Not.ts +16 -0
package/README.md
CHANGED
|
@@ -268,6 +268,38 @@ process.once("SIGTERM", async () => {
|
|
|
268
268
|
The positional forms of earlier versions (e.g. `main("my-app", logger, () => startServer(), 2)`)
|
|
269
269
|
were removed in 0.11.0; passing anything other than an options object throws a `TypeError`.
|
|
270
270
|
|
|
271
|
+
### Type testing
|
|
272
|
+
|
|
273
|
+
Type-level helpers to check types at compile time, e.g. in test files. They are types only, so they
|
|
274
|
+
have no runtime cost and add no dependency to your tests:
|
|
275
|
+
|
|
276
|
+
- `Assert<T>` type-checks only when `T` is exactly `true`.
|
|
277
|
+
- `Equal<X, Y>` is `true` when `X` and `Y` are identical types. It is stricter than mutual
|
|
278
|
+
assignability: `any` only equals `any`, and `readonly` and `?` modifiers must match.
|
|
279
|
+
- `Extends<X, Y>` is `true` when `X` is assignable to `Y` (unions are compared as a whole).
|
|
280
|
+
- `Not<T>` negates a predicate result.
|
|
281
|
+
- `IsAny<T>`, `IsNever<T>`, and `IsUnknown<T>` detect those special types.
|
|
282
|
+
|
|
283
|
+
Declare each assertion as an (unused) type alias; a failing one is reported by `tsc` (e.g.
|
|
284
|
+
`tsc --noEmit`), not by the test runner, since Node.js strips types without checking them. Use
|
|
285
|
+
`@ts-expect-error` to assert that an assertion fails.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
import {
|
|
289
|
+
type Assert,
|
|
290
|
+
asString,
|
|
291
|
+
type Equal,
|
|
292
|
+
type IsUnknown,
|
|
293
|
+
type Not,
|
|
294
|
+
} from "@darthcav/ts-utils"
|
|
295
|
+
|
|
296
|
+
type _AsString = Assert<Equal<ReturnType<typeof asString>, string | undefined>>
|
|
297
|
+
type _NotAny = Assert<Not<Equal<ReturnType<typeof asString>, any>>>
|
|
298
|
+
type _Parsed = Assert<IsUnknown<Parameters<typeof asString>[0]>>
|
|
299
|
+
// @ts-expect-error -- `string` is not `number`
|
|
300
|
+
type _Fails = Assert<Equal<string, number>>
|
|
301
|
+
```
|
|
302
|
+
|
|
271
303
|
For the full API reference see the [API Documentation][pages-url].
|
|
272
304
|
|
|
273
305
|
## Development
|
|
@@ -305,6 +337,7 @@ src/
|
|
|
305
337
|
osRelease.ts # OS release utilities
|
|
306
338
|
types.ts # RuntimeObject type and type guards
|
|
307
339
|
loggers/ # Logger utilities
|
|
340
|
+
type-testing/ # Type-level assertion helpers
|
|
308
341
|
__tests__/ # Test files
|
|
309
342
|
dist/ # Compiled output (generated)
|
|
310
343
|
public/ # Documentation output (generated)
|
|
@@ -316,7 +349,7 @@ public/ # Documentation output (generated)
|
|
|
316
349
|
|
|
317
350
|
[node-version]: https://img.shields.io/badge/node-%3E%3D26-orange.svg?style=flat-square
|
|
318
351
|
[node-url]: https://nodejs.org
|
|
319
|
-
[version-image]: https://img.shields.io/badge/version-0.
|
|
352
|
+
[version-image]: https://img.shields.io/badge/version-0.12.0-blue.svg?style=flat-square
|
|
320
353
|
[ci-badge]: https://github.com/darthcav/ts-utils/actions/workflows/tests.yml/badge.svg
|
|
321
354
|
[coverage-badge]:
|
|
322
355
|
https://codecov.io/github/darthcav/ts-utils/branch/dev/graph/badge.svg?token=RNEPER4KEI
|
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,13 @@ import { millisecondsToString } from "./millisecondsToString.ts";
|
|
|
10
10
|
import { monitorMemory } from "./monitorMemory.ts";
|
|
11
11
|
import { noop } from "./noop.ts";
|
|
12
12
|
import { type OsRelease, osRelease } from "./osRelease.ts";
|
|
13
|
+
import type { Assert } from "./type-testing/Assert.ts";
|
|
14
|
+
import type { Equal } from "./type-testing/Equal.ts";
|
|
15
|
+
import type { Extends } from "./type-testing/Extends.ts";
|
|
16
|
+
import type { IsAny } from "./type-testing/IsAny.ts";
|
|
17
|
+
import type { IsNever } from "./type-testing/IsNever.ts";
|
|
18
|
+
import type { IsUnknown } from "./type-testing/IsUnknown.ts";
|
|
19
|
+
import type { Not } from "./type-testing/Not.ts";
|
|
13
20
|
import { asRuntimeObject, asString, isRuntimeObject, isString, type RuntimeObject, toRuntimeObjectArray } from "./types.ts";
|
|
14
|
-
export { asRuntimeObject, asString, getConsoleLogger, getDummyLogger, isRuntimeObject, isString, type LauncherFunction, type MainOptions, main, millisecondsToString, monitorMemory, noop, type OsRelease, osRelease, type RuntimeObject, toRuntimeObjectArray, };
|
|
21
|
+
export { type Assert, asRuntimeObject, asString, type Equal, type Extends, getConsoleLogger, getDummyLogger, type IsAny, type IsNever, type IsUnknown, isRuntimeObject, isString, type LauncherFunction, type MainOptions, main, millisecondsToString, monitorMemory, type Not, noop, type OsRelease, osRelease, type RuntimeObject, toRuntimeObjectArray, };
|
|
15
22
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,WAAW,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,KAAK,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,EACH,eAAe,EACf,QAAQ,EACR,eAAe,EACf,QAAQ,EACR,KAAK,aAAa,EAClB,oBAAoB,EACvB,MAAM,YAAY,CAAA;AAEnB,OAAO,EACH,eAAe,EACf,QAAQ,EACR,gBAAgB,EAChB,cAAc,EACd,eAAe,EACf,QAAQ,EACR,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,IAAI,EACJ,KAAK,SAAS,EACd,SAAS,EACT,KAAK,aAAa,EAClB,oBAAoB,GACvB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,WAAW,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,KAAK,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AACtD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAA;AACpD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAA;AAChD,OAAO,EACH,eAAe,EACf,QAAQ,EACR,eAAe,EACf,QAAQ,EACR,KAAK,aAAa,EAClB,oBAAoB,EACvB,MAAM,YAAY,CAAA;AAEnB,OAAO,EACH,KAAK,MAAM,EACX,eAAe,EACf,QAAQ,EACR,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,gBAAgB,EAChB,cAAc,EACd,KAAK,KAAK,EACV,KAAK,OAAO,EACZ,KAAK,SAAS,EACd,eAAe,EACf,QAAQ,EACR,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,IAAI,EACJ,oBAAoB,EACpB,aAAa,EACb,KAAK,GAAG,EACR,IAAI,EACJ,KAAK,SAAS,EACd,SAAS,EACT,KAAK,aAAa,EAClB,oBAAoB,GACvB,CAAA"}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAA2C,IAAI,EAAE,MAAM,WAAW,CAAA;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAkB,SAAS,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAChE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5D,OAAO,EAA2C,IAAI,EAAE,MAAM,WAAW,CAAA;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAkB,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAQ1D,OAAO,EACH,eAAe,EACf,QAAQ,EACR,eAAe,EACf,QAAQ,EAER,oBAAoB,GACvB,MAAM,YAAY,CAAA;AAEnB,OAAO,EAEH,eAAe,EACf,QAAQ,EAGR,gBAAgB,EAChB,cAAc,EAId,eAAe,EACf,QAAQ,EAGR,IAAI,EACJ,oBAAoB,EACpB,aAAa,EAEb,IAAI,EAEJ,SAAS,EAET,oBAAoB,GACvB,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compile-time assertion: type-checks only when `T` is exactly `true`.
|
|
3
|
+
*
|
|
4
|
+
* Wrap one of the type-level predicates of this module ({@link Equal},
|
|
5
|
+
* {@link Extends}, {@link Not}, {@link IsAny}, {@link IsNever},
|
|
6
|
+
* {@link IsUnknown}) in it and declare the result as an (unused) type alias.
|
|
7
|
+
* A predicate that evaluates to `false` or `boolean` becomes a type error that
|
|
8
|
+
* `tsc` reports. The alias resolves to `true` and, like every type, is erased
|
|
9
|
+
* at runtime.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The predicate result that must be `true`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Equal } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Ok = Assert<Equal<ReturnType<typeof parseInt>, number>>
|
|
18
|
+
* // @ts-expect-error -- `string` is not `number`
|
|
19
|
+
* type _Fails = Assert<Equal<string, number>>
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type Assert<T extends true> = T;
|
|
23
|
+
//# sourceMappingURL=Assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Assert.d.ts","sourceRoot":"","sources":["../../src/type-testing/Assert.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Assert.js","sourceRoot":"","sources":["../../src/type-testing/Assert.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` and `Y` are identical types, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* The comparison is the one the compiler uses for type identity, so it is
|
|
6
|
+
* stricter than mutual assignability: `any` only equals `any`, and modifiers
|
|
7
|
+
* such as `readonly` and `?` must match. An intersection is not identical to
|
|
8
|
+
* the equivalent flattened object type (`{ a: 1 } & { b: 2 }` is not
|
|
9
|
+
* `{ a: 1; b: 2 }`); flatten it with a mapped type before comparing when
|
|
10
|
+
* needed.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam X - The first type to compare.
|
|
13
|
+
* @typeParam Y - The second type to compare.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
18
|
+
*
|
|
19
|
+
* type _Same = Assert<Equal<{ a: string }, { a: string }>>
|
|
20
|
+
* type _Readonly = Assert<Not<Equal<{ readonly a: string }, { a: string }>>>
|
|
21
|
+
* type _Any = Assert<Not<Equal<any, string>>>
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
25
|
+
//# sourceMappingURL=Equal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Equal.d.ts","sourceRoot":"","sources":["../../src/type-testing/Equal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAClB,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,GAC7D,IAAI,GACJ,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Equal.js","sourceRoot":"","sources":["../../src/type-testing/Equal.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` is assignable to `Y`, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* Unions are compared as a whole rather than member by member, so
|
|
6
|
+
* `Extends<string | number, string>` is `false`. Since `any` and `never` are
|
|
7
|
+
* assignable to every type, `Extends<any, Y>` and `Extends<never, Y>` are
|
|
8
|
+
* always `true`; use {@link Equal} to check for an exact type.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam X - The type to check.
|
|
11
|
+
* @typeParam Y - The type `X` must be assignable to.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Extends, Not } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Literal = Assert<Extends<"a", string>>
|
|
18
|
+
* type _Union = Assert<Not<Extends<string | number, string>>>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export type Extends<X, Y> = [X] extends [Y] ? true : false;
|
|
22
|
+
//# sourceMappingURL=Extends.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Extends.d.ts","sourceRoot":"","sources":["../../src/type-testing/Extends.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Extends.js","sourceRoot":"","sources":["../../src/type-testing/Extends.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `any`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to catch a value or return type that silently widened to `any`.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam T - The type to check.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type { Assert, IsAny, Not } from "@darthcav/ts-utils"
|
|
11
|
+
*
|
|
12
|
+
* type _Parsed = Assert<IsAny<ReturnType<typeof JSON.parse>>>
|
|
13
|
+
* type _Typed = Assert<Not<IsAny<unknown>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
17
|
+
//# sourceMappingURL=IsAny.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsAny.d.ts","sourceRoot":"","sources":["../../src/type-testing/IsAny.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsAny.js","sourceRoot":"","sources":["../../src/type-testing/IsAny.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `never`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to check that a union has been exhausted or that a conditional type
|
|
5
|
+
* filters out every member.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The type to check.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, IsNever } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Empty = Assert<IsNever<Exclude<"a" | "b", string>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsNever<T> = [T] extends [never] ? true : false;
|
|
17
|
+
//# sourceMappingURL=IsNever.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsNever.d.ts","sourceRoot":"","sources":["../../src/type-testing/IsNever.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsNever.js","sourceRoot":"","sources":["../../src/type-testing/IsNever.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { IsAny } from "./IsAny.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Type-level predicate: `true` when `T` is `unknown`, `false` otherwise
|
|
4
|
+
* (including for `any`).
|
|
5
|
+
*
|
|
6
|
+
* Useful to check that an untrusted value (e.g. parsed input) is typed as
|
|
7
|
+
* `unknown` and has to be narrowed before use.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - The type to check.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import type { Assert, IsUnknown, Not } from "@darthcav/ts-utils"
|
|
14
|
+
*
|
|
15
|
+
* type _Unknown = Assert<IsUnknown<unknown>>
|
|
16
|
+
* type _Any = Assert<Not<IsUnknown<any>>>
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type IsUnknown<T> = IsAny<T> extends true ? false : unknown extends T ? true : false;
|
|
20
|
+
//# sourceMappingURL=IsUnknown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsUnknown.d.ts","sourceRoot":"","sources":["../../src/type-testing/IsUnknown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAEvC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IACnB,KAAK,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,KAAK,GAAG,OAAO,SAAS,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsUnknown.js","sourceRoot":"","sources":["../../src/type-testing/IsUnknown.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: negates a boolean predicate result.
|
|
3
|
+
*
|
|
4
|
+
* `Not<true>` is `false` and `Not<false>` is `true`. An undecided `boolean`
|
|
5
|
+
* stays `boolean`, so `Assert<Not<boolean>>` still fails.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The predicate result to negate.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Different = Assert<Not<Equal<string, number>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type Not<T extends boolean> = T extends true ? false : true;
|
|
17
|
+
//# sourceMappingURL=Not.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Not.d.ts","sourceRoot":"","sources":["../../src/type-testing/Not.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,GAAG,CAAC,CAAC,SAAS,OAAO,IAAI,CAAC,SAAS,IAAI,GAAG,KAAK,GAAG,IAAI,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Not.js","sourceRoot":"","sources":["../../src/type-testing/Not.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@darthcav/ts-utils",
|
|
3
3
|
"description": "A collection of utility functions for TypeScript applications and modules",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.12.0",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "darthcav",
|
|
7
7
|
"url": "https://github.com/darthcav"
|
|
@@ -24,14 +24,12 @@
|
|
|
24
24
|
"!dist/__tests__"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@logtape/logtape": "2.3.
|
|
27
|
+
"@logtape/logtape": "2.3.11"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@biomejs/biome": "2.5.15",
|
|
31
31
|
"@types/node": "26.6.4",
|
|
32
|
-
"asserttt": "1.0.1",
|
|
33
32
|
"prettier": "3.9.9",
|
|
34
|
-
"shx": "0.4.0",
|
|
35
33
|
"typedoc": "0.28.20"
|
|
36
34
|
},
|
|
37
35
|
"repository": {
|
|
@@ -43,7 +41,7 @@
|
|
|
43
41
|
},
|
|
44
42
|
"scripts": {
|
|
45
43
|
"\n===== Build =====": "",
|
|
46
|
-
"clean": "
|
|
44
|
+
"clean": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
|
|
47
45
|
"build": "npm run clean && tsc",
|
|
48
46
|
"prepare": "npm run build",
|
|
49
47
|
"\n===== Typecheck =====": "",
|
|
@@ -51,11 +49,11 @@
|
|
|
51
49
|
"\n===== Test =====": "",
|
|
52
50
|
"test": "node --env-file-if-exists=.env.local --experimental-test-module-mocks --test --test-reporter=spec \"src/**/*.test.ts\"",
|
|
53
51
|
"test:coverage": "node --env-file-if-exists=.env.local --experimental-test-module-mocks --experimental-test-coverage --test \"src/**/*.test.ts\"",
|
|
54
|
-
"test:coverage:lcov": "
|
|
52
|
+
"test:coverage:lcov": "node -e \"fs.mkdirSync('coverage', { recursive: true })\" && node --env-file-if-exists=.env.local --experimental-test-module-mocks --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-reporter=spec --test-reporter-destination=stdout --test \"src/**/*.test.ts\"",
|
|
55
53
|
"\n===== Linter =====": "",
|
|
56
54
|
"lint": "biome check . && prettier --check \"**/*.md\" --ignore-path .gitignore",
|
|
57
55
|
"lint:fix": "biome check --write . && prettier --write \"**/*.md\" --ignore-path .gitignore",
|
|
58
56
|
"\n===== TypeDoc =====": "",
|
|
59
|
-
"doc": "
|
|
57
|
+
"doc": "typedoc --tsconfig tsconfig.json"
|
|
60
58
|
}
|
|
61
59
|
}
|
package/src/index.ts
CHANGED
|
@@ -11,6 +11,13 @@ import { millisecondsToString } from "./millisecondsToString.ts"
|
|
|
11
11
|
import { monitorMemory } from "./monitorMemory.ts"
|
|
12
12
|
import { noop } from "./noop.ts"
|
|
13
13
|
import { type OsRelease, osRelease } from "./osRelease.ts"
|
|
14
|
+
import type { Assert } from "./type-testing/Assert.ts"
|
|
15
|
+
import type { Equal } from "./type-testing/Equal.ts"
|
|
16
|
+
import type { Extends } from "./type-testing/Extends.ts"
|
|
17
|
+
import type { IsAny } from "./type-testing/IsAny.ts"
|
|
18
|
+
import type { IsNever } from "./type-testing/IsNever.ts"
|
|
19
|
+
import type { IsUnknown } from "./type-testing/IsUnknown.ts"
|
|
20
|
+
import type { Not } from "./type-testing/Not.ts"
|
|
14
21
|
import {
|
|
15
22
|
asRuntimeObject,
|
|
16
23
|
asString,
|
|
@@ -21,10 +28,16 @@ import {
|
|
|
21
28
|
} from "./types.ts"
|
|
22
29
|
|
|
23
30
|
export {
|
|
31
|
+
type Assert,
|
|
24
32
|
asRuntimeObject,
|
|
25
33
|
asString,
|
|
34
|
+
type Equal,
|
|
35
|
+
type Extends,
|
|
26
36
|
getConsoleLogger,
|
|
27
37
|
getDummyLogger,
|
|
38
|
+
type IsAny,
|
|
39
|
+
type IsNever,
|
|
40
|
+
type IsUnknown,
|
|
28
41
|
isRuntimeObject,
|
|
29
42
|
isString,
|
|
30
43
|
type LauncherFunction,
|
|
@@ -32,6 +45,7 @@ export {
|
|
|
32
45
|
main,
|
|
33
46
|
millisecondsToString,
|
|
34
47
|
monitorMemory,
|
|
48
|
+
type Not,
|
|
35
49
|
noop,
|
|
36
50
|
type OsRelease,
|
|
37
51
|
osRelease,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compile-time assertion: type-checks only when `T` is exactly `true`.
|
|
3
|
+
*
|
|
4
|
+
* Wrap one of the type-level predicates of this module ({@link Equal},
|
|
5
|
+
* {@link Extends}, {@link Not}, {@link IsAny}, {@link IsNever},
|
|
6
|
+
* {@link IsUnknown}) in it and declare the result as an (unused) type alias.
|
|
7
|
+
* A predicate that evaluates to `false` or `boolean` becomes a type error that
|
|
8
|
+
* `tsc` reports. The alias resolves to `true` and, like every type, is erased
|
|
9
|
+
* at runtime.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The predicate result that must be `true`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Equal } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Ok = Assert<Equal<ReturnType<typeof parseInt>, number>>
|
|
18
|
+
* // @ts-expect-error -- `string` is not `number`
|
|
19
|
+
* type _Fails = Assert<Equal<string, number>>
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type Assert<T extends true> = T
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` and `Y` are identical types, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* The comparison is the one the compiler uses for type identity, so it is
|
|
6
|
+
* stricter than mutual assignability: `any` only equals `any`, and modifiers
|
|
7
|
+
* such as `readonly` and `?` must match. An intersection is not identical to
|
|
8
|
+
* the equivalent flattened object type (`{ a: 1 } & { b: 2 }` is not
|
|
9
|
+
* `{ a: 1; b: 2 }`); flatten it with a mapped type before comparing when
|
|
10
|
+
* needed.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam X - The first type to compare.
|
|
13
|
+
* @typeParam Y - The second type to compare.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
18
|
+
*
|
|
19
|
+
* type _Same = Assert<Equal<{ a: string }, { a: string }>>
|
|
20
|
+
* type _Readonly = Assert<Not<Equal<{ readonly a: string }, { a: string }>>>
|
|
21
|
+
* type _Any = Assert<Not<Equal<any, string>>>
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type Equal<X, Y> =
|
|
25
|
+
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2
|
|
26
|
+
? true
|
|
27
|
+
: false
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` is assignable to `Y`, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* Unions are compared as a whole rather than member by member, so
|
|
6
|
+
* `Extends<string | number, string>` is `false`. Since `any` and `never` are
|
|
7
|
+
* assignable to every type, `Extends<any, Y>` and `Extends<never, Y>` are
|
|
8
|
+
* always `true`; use {@link Equal} to check for an exact type.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam X - The type to check.
|
|
11
|
+
* @typeParam Y - The type `X` must be assignable to.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Extends, Not } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Literal = Assert<Extends<"a", string>>
|
|
18
|
+
* type _Union = Assert<Not<Extends<string | number, string>>>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export type Extends<X, Y> = [X] extends [Y] ? true : false
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `any`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to catch a value or return type that silently widened to `any`.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam T - The type to check.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type { Assert, IsAny, Not } from "@darthcav/ts-utils"
|
|
11
|
+
*
|
|
12
|
+
* type _Parsed = Assert<IsAny<ReturnType<typeof JSON.parse>>>
|
|
13
|
+
* type _Typed = Assert<Not<IsAny<unknown>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `never`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to check that a union has been exhausted or that a conditional type
|
|
5
|
+
* filters out every member.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The type to check.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, IsNever } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Empty = Assert<IsNever<Exclude<"a" | "b", string>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsNever<T> = [T] extends [never] ? true : false
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IsAny } from "./IsAny.ts"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type-level predicate: `true` when `T` is `unknown`, `false` otherwise
|
|
5
|
+
* (including for `any`).
|
|
6
|
+
*
|
|
7
|
+
* Useful to check that an untrusted value (e.g. parsed input) is typed as
|
|
8
|
+
* `unknown` and has to be narrowed before use.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam T - The type to check.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import type { Assert, IsUnknown, Not } from "@darthcav/ts-utils"
|
|
15
|
+
*
|
|
16
|
+
* type _Unknown = Assert<IsUnknown<unknown>>
|
|
17
|
+
* type _Any = Assert<Not<IsUnknown<any>>>
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export type IsUnknown<T> =
|
|
21
|
+
IsAny<T> extends true ? false : unknown extends T ? true : false
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: negates a boolean predicate result.
|
|
3
|
+
*
|
|
4
|
+
* `Not<true>` is `false` and `Not<false>` is `true`. An undecided `boolean`
|
|
5
|
+
* stays `boolean`, so `Assert<Not<boolean>>` still fails.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The predicate result to negate.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Different = Assert<Not<Equal<string, number>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type Not<T extends boolean> = T extends true ? false : true
|