type-plus 4.3.1 → 4.4.1
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 +72 -71
- package/cjs/index.d.ts +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -1
- package/cjs/testing/index.d.ts +1 -0
- package/cjs/testing/index.js +18 -0
- package/cjs/testing/index.js.map +1 -0
- package/cjs/testing/stub.d.ts +4 -0
- package/cjs/testing/stub.js +8 -0
- package/cjs/testing/stub.js.map +1 -0
- package/cjs/utils/as.d.ts +2 -2
- package/cjs/utils/as.js +2 -2
- package/cjs/utils/as.js.map +1 -1
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/testing/index.js +2 -0
- package/esm/testing/index.js.map +1 -0
- package/esm/testing/stub.js +4 -0
- package/esm/testing/stub.js.map +1 -0
- package/esm/utils/as.js +2 -2
- package/esm/utils/as.js.map +1 -1
- package/package.json +6 -9
- package/ts/index.ts +1 -0
- package/ts/object/run.ts +0 -0
- package/ts/testing/index.ts +1 -0
- package/ts/testing/stub.spec.ts +38 -0
- package/ts/testing/stub.ts +8 -0
- package/ts/utils/as.spec.ts +33 -24
- package/ts/utils/as.ts +7 -7
package/README.md
CHANGED
|
@@ -15,9 +15,10 @@ Provides additional types and type adjusted utilities for [TypeScript](https://w
|
|
|
15
15
|
|
|
16
16
|
## Feature Highlights
|
|
17
17
|
|
|
18
|
-
- [Type
|
|
19
|
-
- [Nominal Types](#nominal-type)
|
|
18
|
+
- [Type assertions](#type-assertions)
|
|
20
19
|
- [Type Utilities](#type-utilities)
|
|
20
|
+
- [Nominal Types](#nominal-types)
|
|
21
|
+
- [Functional Types](#functional-types)
|
|
21
22
|
|
|
22
23
|
## Installation
|
|
23
24
|
|
|
@@ -27,12 +28,11 @@ npm install type-plus
|
|
|
27
28
|
yarn add type-plus
|
|
28
29
|
```
|
|
29
30
|
|
|
30
|
-
## Type
|
|
31
|
+
## Type Assertions
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
`type-plus` also provides a few other ways to do type assertions.
|
|
33
|
+
Type assertion is one of the main features of `type-plus`.
|
|
34
34
|
|
|
35
|
-
There are
|
|
35
|
+
There are 5 kinds of type assertions:
|
|
36
36
|
|
|
37
37
|
- `runtime`: validates during runtime.
|
|
38
38
|
- `immediate`: validates at compile time.
|
|
@@ -264,70 +264,6 @@ assertType.isTrue(notA({ a: 1 }))
|
|
|
264
264
|
notA({ a: '' }) // TypeScript complains
|
|
265
265
|
```
|
|
266
266
|
|
|
267
|
-
## Nominal Type
|
|
268
|
-
|
|
269
|
-
The TypeScript type system is structural.
|
|
270
|
-
|
|
271
|
-
In some cases, we want to express a type with nominal behavior.
|
|
272
|
-
`type-plus` provides two kinds of nominal types: `Brand` and `Flavor`.
|
|
273
|
-
|
|
274
|
-
`Brand<B, T>`:
|
|
275
|
-
|
|
276
|
-
`brand(type, subject?)`:
|
|
277
|
-
|
|
278
|
-
Branded nominal type is the stronger nominal type of the two.
|
|
279
|
-
It disallows unbranded type assigned to it:
|
|
280
|
-
|
|
281
|
-
```ts
|
|
282
|
-
const a = brand('a', { a: 1 })
|
|
283
|
-
const b = { a: 1 }
|
|
284
|
-
a = b // error
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
`subject` can be any type, from primitive to strings to objects.
|
|
288
|
-
|
|
289
|
-
`brand(type)`:
|
|
290
|
-
|
|
291
|
-
If you do not provide `subject`, `brand(type)` will return a brand creator,
|
|
292
|
-
so that you can use it to create multiple branded values:
|
|
293
|
-
|
|
294
|
-
```ts
|
|
295
|
-
const nike = brand('nike')
|
|
296
|
-
const shirt = nike('shirt')
|
|
297
|
-
const socks = nike('socks')
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
`Flavor<F, T>`:
|
|
301
|
-
|
|
302
|
-
`flavor(type, subject?)`:
|
|
303
|
-
|
|
304
|
-
The key difference between `Flavor` and `Brand` is that
|
|
305
|
-
unflavored type can be assigned to `Flavor`:
|
|
306
|
-
|
|
307
|
-
```ts
|
|
308
|
-
let f = flavor('orange', 'soda')
|
|
309
|
-
f = 'mist' // ok
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
Also, `Brand` of the same name can be assigned to `Flavor`,
|
|
313
|
-
but `Flavor` of the same name cannot be assigned to `Brand`.
|
|
314
|
-
|
|
315
|
-
`nominalMatch(a, b)`:
|
|
316
|
-
|
|
317
|
-
`nominalMatch()` can be used to compare `Brand` or `Flavor`.
|
|
318
|
-
|
|
319
|
-
```ts
|
|
320
|
-
const b1 = brand('x', 1)
|
|
321
|
-
const b2 = brand('y', 1)
|
|
322
|
-
|
|
323
|
-
nominalMatch(b1, b2) // false
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
## Functional Types
|
|
327
|
-
|
|
328
|
-
- `ChainFn<T>: T`: chain function that returns the input type.
|
|
329
|
-
- `compose(...fns): F`: compose functions
|
|
330
|
-
|
|
331
267
|
## Type Utilities
|
|
332
268
|
|
|
333
269
|
`type-plus` also provides additional type utilities.
|
|
@@ -415,7 +351,7 @@ JSONTypes.get<string>(someJson, 'a', 'b', 1, 'c') // miku
|
|
|
415
351
|
|
|
416
352
|
- `ANotB<A, B>`: get object with properties in `A` and not in `B`, including properties with a different value type.
|
|
417
353
|
- `BNotA<A, B>`: flip of `ANotB`
|
|
418
|
-
- `as<T>(subject)`: assert `subject` as `T`. Avoid ASI issues such as `;(x as
|
|
354
|
+
- `as<T>(subject)`: assert `subject` as `T`. Avoid ASI issues such as `;(x as T).abc`
|
|
419
355
|
- `asAny(subject)`: assert `subject` as `any`. Avoid ASI issue such as `;(x as any).abc`
|
|
420
356
|
- `Except<T, K>`: Deprecated. Same as `Omit<T, K>`.
|
|
421
357
|
- `ExcludePropType<T, U>`: excludes type `U` from properties in `T`.
|
|
@@ -496,6 +432,7 @@ So you may encounter some weird behavior if your logic is complex.
|
|
|
496
432
|
- `required(...)`: merge options and removing `Partial<T>`. From [`unpartial`](https://github.com/unional/unpartial)
|
|
497
433
|
- `requiredDeep(...)`: merge options deeply and removing `Partial<T>`. From [`unpartial`](https://github.com/unional/unpartial)
|
|
498
434
|
- `split(target, ...splitters)`: split one object into multiple objects.
|
|
435
|
+
- `stub<T>(value)`: stub a particular type `T`.
|
|
499
436
|
- `typeOverrideIncompatible<T>()`: override only the incompatible portion between two types.
|
|
500
437
|
|
|
501
438
|
```ts
|
|
@@ -516,6 +453,70 @@ const source = {
|
|
|
516
453
|
overrider(source, { foo: !!source.foo })
|
|
517
454
|
```
|
|
518
455
|
|
|
456
|
+
## Nominal Types
|
|
457
|
+
|
|
458
|
+
The TypeScript type system is structural.
|
|
459
|
+
|
|
460
|
+
In some cases, we want to express a type with nominal behavior.
|
|
461
|
+
`type-plus` provides two kinds of nominal types: `Brand` and `Flavor`.
|
|
462
|
+
|
|
463
|
+
`Brand<B, T>`:
|
|
464
|
+
|
|
465
|
+
`brand(type, subject?)`:
|
|
466
|
+
|
|
467
|
+
Branded nominal type is the stronger nominal type of the two.
|
|
468
|
+
It disallows unbranded type assigned to it:
|
|
469
|
+
|
|
470
|
+
```ts
|
|
471
|
+
const a = brand('a', { a: 1 })
|
|
472
|
+
const b = { a: 1 }
|
|
473
|
+
a = b // error
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
`subject` can be any type, from primitive to strings to objects.
|
|
477
|
+
|
|
478
|
+
`brand(type)`:
|
|
479
|
+
|
|
480
|
+
If you do not provide `subject`, `brand(type)` will return a brand creator,
|
|
481
|
+
so that you can use it to create multiple branded values:
|
|
482
|
+
|
|
483
|
+
```ts
|
|
484
|
+
const nike = brand('nike')
|
|
485
|
+
const shirt = nike('shirt')
|
|
486
|
+
const socks = nike('socks')
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
`Flavor<F, T>`:
|
|
490
|
+
|
|
491
|
+
`flavor(type, subject?)`:
|
|
492
|
+
|
|
493
|
+
The key difference between `Flavor` and `Brand` is that
|
|
494
|
+
unflavored type can be assigned to `Flavor`:
|
|
495
|
+
|
|
496
|
+
```ts
|
|
497
|
+
let f = flavor('orange', 'soda')
|
|
498
|
+
f = 'mist' // ok
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
Also, `Brand` of the same name can be assigned to `Flavor`,
|
|
502
|
+
but `Flavor` of the same name cannot be assigned to `Brand`.
|
|
503
|
+
|
|
504
|
+
`nominalMatch(a, b)`:
|
|
505
|
+
|
|
506
|
+
`nominalMatch()` can be used to compare `Brand` or `Flavor`.
|
|
507
|
+
|
|
508
|
+
```ts
|
|
509
|
+
const b1 = brand('x', 1)
|
|
510
|
+
const b2 = brand('y', 1)
|
|
511
|
+
|
|
512
|
+
nominalMatch(b1, b2) // false
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
## Functional Types
|
|
516
|
+
|
|
517
|
+
- `ChainFn<T>: T`: chain function that returns the input type.
|
|
518
|
+
- `compose(...fns): F`: compose functions
|
|
519
|
+
|
|
519
520
|
## Attribution
|
|
520
521
|
|
|
521
522
|
Some of the code in this library is created by other people in the TypeScript community.
|
package/cjs/index.d.ts
CHANGED
package/cjs/index.js
CHANGED
|
@@ -31,6 +31,7 @@ __exportStar(require("./nominal-types"), exports);
|
|
|
31
31
|
__exportStar(require("./object"), exports);
|
|
32
32
|
__exportStar(require("./predicates"), exports);
|
|
33
33
|
__exportStar(require("./promise"), exports);
|
|
34
|
+
__exportStar(require("./testing"), exports);
|
|
34
35
|
__exportStar(require("./utils"), exports);
|
|
35
36
|
// export { types, types as T }
|
|
36
37
|
//# sourceMappingURL=index.js.map
|
package/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mCAAmC;AACnC,uCAAkD;AAAzC,qGAAA,QAAQ,OAAA;AAAE,yGAAA,YAAY,OAAA;AAC/B,0CAAuB;AACvB,8CAA2B;AAC3B,0CAAuB;AAEvB,6CAA0B;AAC1B,+CAA4B;AAC5B,8CAA2B;AAC3B,yCAAsB;AACtB,2CAAwB;AACxB,kDAA+B;AAC/B,2CAAwB;AACxB,+CAA4B;AAE5B,4CAAyB;AAIzB,0CAAuB;AACvB,+BAA+B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,mCAAmC;AACnC,uCAAkD;AAAzC,qGAAA,QAAQ,OAAA;AAAE,yGAAA,YAAY,OAAA;AAC/B,0CAAuB;AACvB,8CAA2B;AAC3B,0CAAuB;AAEvB,6CAA0B;AAC1B,+CAA4B;AAC5B,8CAA2B;AAC3B,yCAAsB;AACtB,2CAAwB;AACxB,kDAA+B;AAC/B,2CAAwB;AACxB,+CAA4B;AAE5B,4CAAyB;AACzB,4CAAyB;AAIzB,0CAAuB;AACvB,+BAA+B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './stub';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./stub"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/testing/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAsB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stub.js","sourceRoot":"","sources":["../../ts/testing/stub.ts"],"names":[],"mappings":";;;AAKA,SAAgB,IAAI,CAAI,IAAc;IACpC,OAAO,IAAS,CAAA;AAClB,CAAC;AAFD,oBAEC"}
|
package/cjs/utils/as.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function as<T>(subject: unknown):
|
|
2
|
-
export declare function asAny(subject: unknown):
|
|
1
|
+
export declare function as<T>(subject: unknown): T;
|
|
2
|
+
export declare function asAny(subject: unknown): any;
|
package/cjs/utils/as.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.asAny = exports.as = void 0;
|
|
4
4
|
function as(subject) {
|
|
5
|
-
return
|
|
5
|
+
return subject;
|
|
6
6
|
}
|
|
7
7
|
exports.as = as;
|
|
8
8
|
function asAny(subject) {
|
|
9
|
-
return
|
|
9
|
+
return subject;
|
|
10
10
|
}
|
|
11
11
|
exports.asAny = asAny;
|
|
12
12
|
//# sourceMappingURL=as.js.map
|
package/cjs/utils/as.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"as.js","sourceRoot":"","sources":["../../ts/utils/as.ts"],"names":[],"mappings":";;;AAAA,SAAgB,EAAE,CAAI,OAAgB;IACpC,OAAO,
|
|
1
|
+
{"version":3,"file":"as.js","sourceRoot":"","sources":["../../ts/utils/as.ts"],"names":[],"mappings":";;;AAAA,SAAgB,EAAE,CAAI,OAAgB;IACpC,OAAO,OAAY,CAAA;AACrB,CAAC;AAFD,gBAEC;AAED,SAAgB,KAAK,CAAC,OAAgB;IACpC,OAAO,OAAO,CAAA;AAChB,CAAC;AAFD,sBAEC"}
|
package/esm/index.js
CHANGED
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAClD,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AAEvB,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA;AACtB,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAE5B,cAAc,WAAW,CAAA;AAIzB,cAAc,SAAS,CAAA;AACvB,+BAA+B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../ts/index.ts"],"names":[],"mappings":"AAAA,mCAAmC;AACnC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAClD,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AAEvB,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA;AACtB,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAE5B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AAIzB,cAAc,SAAS,CAAA;AACvB,+BAA+B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/testing/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stub.js","sourceRoot":"","sources":["../../ts/testing/stub.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,IAAI,CAAI,IAAc;IACpC,OAAO,IAAS,CAAA;AAClB,CAAC"}
|
package/esm/utils/as.js
CHANGED
package/esm/utils/as.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"as.js","sourceRoot":"","sources":["../../ts/utils/as.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,EAAE,CAAI,OAAgB;IACpC,OAAO,
|
|
1
|
+
{"version":3,"file":"as.js","sourceRoot":"","sources":["../../ts/utils/as.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,EAAE,CAAI,OAAgB;IACpC,OAAO,OAAY,CAAA;AACrB,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,OAAgB;IACpC,OAAO,OAAO,CAAA;AAChB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-plus",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.1",
|
|
4
4
|
"description": "Provides additional types for TypeScript.",
|
|
5
5
|
"homepage": "https://github.com/unional/type-plus",
|
|
6
6
|
"bugs": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"ts"
|
|
27
27
|
],
|
|
28
28
|
"scripts": {
|
|
29
|
-
"build": "run-p build:cjs build:esm
|
|
29
|
+
"build": "run-p build:cjs build:esm",
|
|
30
30
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
31
31
|
"build:esm": "tsc -p tsconfig.esm.json",
|
|
32
32
|
"build:doc": "typedoc",
|
|
@@ -44,12 +44,8 @@
|
|
|
44
44
|
"test:types": "tsc",
|
|
45
45
|
"verify": "run-p verify:build lint test:types coverage",
|
|
46
46
|
"verify:build": "npm-run-all clean build --parallel dependency-check size-limit",
|
|
47
|
-
"watch": "jest --watch"
|
|
48
|
-
|
|
49
|
-
"husky": {
|
|
50
|
-
"hooks": {
|
|
51
|
-
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
|
52
|
-
}
|
|
47
|
+
"watch": "jest --watch",
|
|
48
|
+
"prepare": "husky install"
|
|
53
49
|
},
|
|
54
50
|
"dependencies": {
|
|
55
51
|
"tersify": "^3.8.2",
|
|
@@ -72,7 +68,7 @@
|
|
|
72
68
|
"dependency-check": "^4.1.0",
|
|
73
69
|
"eslint": "^8.11.0",
|
|
74
70
|
"eslint-plugin-harmony": "^6.0.0",
|
|
75
|
-
"husky": "^7.0.
|
|
71
|
+
"husky": "^7.0.0",
|
|
76
72
|
"jest": "^27.5.1",
|
|
77
73
|
"jest-progress-tracker": "^3.0.3",
|
|
78
74
|
"jest-validate": "^27.5.1",
|
|
@@ -85,6 +81,7 @@
|
|
|
85
81
|
"satisfier": "^5.1.2",
|
|
86
82
|
"size-limit": "^7.0.8",
|
|
87
83
|
"typedoc": "^0.22.13",
|
|
84
|
+
"typedoc-plugin-extras": "^2.2.3",
|
|
88
85
|
"typescript": "^4.6.2"
|
|
89
86
|
},
|
|
90
87
|
"size-limit": [
|
package/ts/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ export * from './object'
|
|
|
14
14
|
export * from './predicates'
|
|
15
15
|
export type { PrimitiveTypes } from './PrimitiveTypes'
|
|
16
16
|
export * from './promise'
|
|
17
|
+
export * from './testing'
|
|
17
18
|
// export * from './types/optional'
|
|
18
19
|
// export * from './types/required'
|
|
19
20
|
export type { UnionKeys } from './UnionKeys'
|
package/ts/object/run.ts
ADDED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './stub'
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isType, stub } from '..'
|
|
2
|
+
|
|
3
|
+
test('stub is a partial of the actual object', () => {
|
|
4
|
+
const real = { a: 1, b: { c: 2 }, d: 3 }
|
|
5
|
+
|
|
6
|
+
const a = stub<typeof real>({ a: 2 })
|
|
7
|
+
expect(a.a).toBe(2)
|
|
8
|
+
isType.equal<true, typeof a, typeof real>()
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('stub function retains param types', () => {
|
|
12
|
+
function real(a: string, b: number): boolean { return a === String(b) }
|
|
13
|
+
|
|
14
|
+
const a = stub<typeof real>((_a, _b) => {
|
|
15
|
+
isType.equal<true, string, typeof _a>()
|
|
16
|
+
isType.equal<true, number, typeof _b>()
|
|
17
|
+
return false
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
expect(a('1', 1)).toBe(false)
|
|
21
|
+
isType.equal<true, typeof a, typeof real>()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('stub function params can be omitted', () => {
|
|
25
|
+
function real(a: string, b: number): boolean { return a === String(b) }
|
|
26
|
+
|
|
27
|
+
const a = stub<typeof real>(() => false)
|
|
28
|
+
|
|
29
|
+
expect(a('1', 1)).toBe(false)
|
|
30
|
+
isType.equal<true, typeof a, typeof real>()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('stub input can be omitted', () => {
|
|
34
|
+
const real = { a: 1, b: { c: 2 }, d: 3 }
|
|
35
|
+
|
|
36
|
+
const a = stub<typeof real>()
|
|
37
|
+
isType.equal<true, typeof a, typeof real>()
|
|
38
|
+
})
|
package/ts/utils/as.spec.ts
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
|
-
import { as, asAny, isType } from '..'
|
|
2
|
-
|
|
3
|
-
describe('as<T>()', () => {
|
|
4
|
-
test('defaults subject type to unknown', () => {
|
|
5
|
-
const s: any = {}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
})
|
|
24
|
-
|
|
1
|
+
import { as, asAny, isType } from '..'
|
|
2
|
+
|
|
3
|
+
describe('as<T>()', () => {
|
|
4
|
+
test('defaults subject type to unknown', () => {
|
|
5
|
+
const s: any = {}
|
|
6
|
+
const a = as(s)
|
|
7
|
+
isType.equal<true, unknown, typeof a>()
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('cast type to T', () => {
|
|
11
|
+
const s: any = {}
|
|
12
|
+
const n = as<number>(s)
|
|
13
|
+
isType.equal<true, number, typeof n>()
|
|
14
|
+
|
|
15
|
+
const str = as<string>(s)
|
|
16
|
+
isType.equal<true, string, typeof str>()
|
|
17
|
+
|
|
18
|
+
const o = as<{ a: number }>(s)
|
|
19
|
+
isType.equal<true, { a: number }, typeof o>()
|
|
20
|
+
|
|
21
|
+
const any = as<any>(s)
|
|
22
|
+
isType.equal<true, any, typeof any>()
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('asAny()', () => {
|
|
27
|
+
test('cast type to any', () => {
|
|
28
|
+
const s: unknown = {}
|
|
29
|
+
const a = asAny(s)
|
|
30
|
+
isType.equal<true, any, typeof a>()
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
package/ts/utils/as.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export function as<T>(subject: unknown):
|
|
2
|
-
return
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function asAny(subject: unknown):
|
|
6
|
-
return
|
|
7
|
-
}
|
|
1
|
+
export function as<T>(subject: unknown): T {
|
|
2
|
+
return subject as T
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function asAny(subject: unknown): any {
|
|
6
|
+
return subject
|
|
7
|
+
}
|