type-plus 3.13.1 → 3.14.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 CHANGED
@@ -35,8 +35,8 @@ yarn add type-plus
35
35
  ## Runtime type checker
36
36
 
37
37
  Bringing the power of TypeScript to JavaScript runtime.
38
- At the moment, this provide some basic functionalities.
39
- If you need more feature, I would recommend other excellent type checking library such as [zod](https://github.com/colinhacks/zod).
38
+ At the moment, this provides some basic functionalities.
39
+ If you need more features, I would recommend other excellent type checking libraries such as [zod](https://github.com/colinhacks/zod).
40
40
 
41
41
  ```ts
42
42
  const eslintConfig = T.object.create({
@@ -74,13 +74,13 @@ Types supported: `any`, `array`, `boolean`, `null`, `number`, `object`, `record`
74
74
  i.e., most of the basic types are supported except `bigint`.
75
75
  It is left out for backward compatibility reasons.
76
76
 
77
- You can use one of the three functions to perform type check:
77
+ You can use one of the three functions to perform type-check:
78
78
 
79
79
  `satisfy(type, subject)`:
80
80
  A loose type check that permits extra elements in `Tuple` and properties in `Object`.
81
81
 
82
82
  `conform(type, subject)`:
83
- A strick type check that does not allow extra elements in `Tuple` and properties in `Object`.
83
+ A strict type check that does not allow extra elements in `Tuple` and properties in `Object`.
84
84
 
85
85
  `check(options, type, subject)`:
86
86
  A general form of `satisfy()` and `conform()`.
@@ -107,7 +107,7 @@ Use the one that fits your specific needs.
107
107
  ✔️ `immediate`
108
108
 
109
109
  It ensures `subject` satisfies `T`.
110
- It is similar to `const x: T = subject` without introducing unused variable.
110
+ It is similar to `const x: T = subject` without introducing an unused variable.
111
111
  You need to specify `T` for it to work.
112
112
 
113
113
  `assertType<T>(subject, validator)`:
@@ -116,7 +116,7 @@ You need to specify `T` for it to work.
116
116
 
117
117
  ✔️ `assertion function`, `runtime`
118
118
 
119
- These overloads of `assertType` allows you to specify a `validator`.
119
+ These overloads of `assertType` allow you to specify a `validator`.
120
120
  With these overloads, `subject` can be `unknown` or `any`.
121
121
 
122
122
  If `subject` fails the assertion,
@@ -163,7 +163,7 @@ const s: number | undefined = undefined
163
163
  assertType.isUndefined(s) // TypeScript complains
164
164
  ```
165
165
 
166
- They accepts `any` and will be narrowed to the specific type.
166
+ They accept `any` and will be narrowed to the specific type.
167
167
 
168
168
  ```ts
169
169
  const s: any = undefined
@@ -177,9 +177,9 @@ s // type is undefined
177
177
 
178
178
  Check if the subject type is `never`.
179
179
  This function is not very useful in actual code as TypeScript will indicate the error.
180
- But it can be useful when writing tests for type.
180
+ But it can be useful when writing tests for types.
181
181
 
182
- This is useful for variable. For type level only check, do the following:
182
+ This is useful for variables. For type level only check, do the following:
183
183
 
184
184
  ```ts
185
185
  assertType.isTrue(true as Equal<YourType, never>)
@@ -214,8 +214,8 @@ const s: number | undefined = 1
214
214
  assertType.noUndefined(s) // TypeScript complains
215
215
  ```
216
216
 
217
- They accepts `subject` with type `any` or `unknown`,
218
- assertion will happens in runtime to ensure `subject` is the specific type.
217
+ They accept `subject` with type `any` or `unknown`,
218
+ the assertion will happen in runtime to ensure `subject` is the specific type.
219
219
 
220
220
  `isType<T>(subject: T)`:
221
221
 
@@ -250,11 +250,11 @@ It returns `true` when passes (which is the only case when used in TypeScript).
250
250
  ✔️ `immediate`
251
251
 
252
252
  Slightly easier to use then `isType.t<>()` and `isType.f<>()`,
253
- when doing type level only equality comparison as you don't have to import `Equal<>`.
253
+ when doing type-level only equality comparison as you don't have to import `Equal<>`.
254
254
 
255
255
  ✔️ `type guard`, `runtime`
256
256
 
257
- These overloads of `isType` allows you to specify a `validator`.
257
+ These overloads of `isType` allow you to specify a `validator`.
258
258
  With these overloads, `subject` can be `unknown` or `any`.
259
259
 
260
260
  `Equal<A, B>`:
@@ -302,7 +302,7 @@ assertType.isTrue(true as CanAssign<{ a:string, b:number }, { a: string }>)
302
302
 
303
303
  ✔️ `immediate`, `logical`
304
304
 
305
- Returns a compile time validating function to ensure `subject` is assignable to `T`.
305
+ Returns a compile-time validating function to ensure `subject` is assignable to `T`.
306
306
 
307
307
  ```ts
308
308
  const isConfig = canAssign<{ a: string }>()
@@ -313,7 +313,7 @@ assertType.isTrue(isConfig({ a: 'a' }))
313
313
 
314
314
  ✔️ `immediate`, `logical`
315
315
 
316
- Returns a compile time validating function to ensure `subject` is not assignable to `T`.
316
+ Returns a compile-time validating function to ensure `subject` is not assignable to `T`.
317
317
 
318
318
  ```ts
319
319
  const notA = canAssign<{ a: string }>(false)
@@ -324,7 +324,7 @@ notA({ a: '' }) // TypeScript complains
324
324
 
325
325
  ## Nominal Type
326
326
 
327
- TypeScript type system is structural.
327
+ The TypeScript type system is structural.
328
328
 
329
329
  In some cases, we want to express a type with nominal behavior.
330
330
  `type-plus` provides two kinds of nominal types: `Brand` and `Flavor`.
@@ -383,12 +383,12 @@ nominalMatch(b1, b2) // false
383
383
 
384
384
  ## Functional Types
385
385
 
386
- - `ChainFn<T>: T`: chain function that return the input type.
386
+ - `ChainFn<T>: T`: chain function that returns the input type.
387
387
 
388
388
  ## Type Utilities
389
389
 
390
390
  `type-plus` also provides additional type utilities.
391
- These utilities includes utility types and type adjusted functions.
391
+ These utilities include utility types and type-adjusted functions.
392
392
 
393
393
  Note that most `predicate` types (such as `IsAny<>`) have a `Then` and `Else` that you can override.
394
394
 
@@ -406,13 +406,13 @@ type No = IsAny<1, 'yes', 'no'> // 'no'
406
406
  - `CreateTuple<L, T>`: creates `Tuple<T>` with `L` number of elements.
407
407
  - `DropFirst<A>`: drops the first value type of `A`.
408
408
  - `DropLast<A>`: drops the last value type of `A`.
409
- - `Filter<A, Criteria>`: gets array of types satisfying `Criteria` in `A`.
410
- - `FindFirst<A, Criteria>`: gets first type satisfying `Criteria`.
411
- - `FindLast<A, Criteria>`: gets last type satisfying `Criteria`.
409
+ - `Filter<A, Criteria>`: gets the array of types satisfying `Criteria` in `A`.
410
+ - `FindFirst<A, Criteria>`: gets the first type satisfying `Criteria`.
411
+ - `FindLast<A, Criteria>`: gets the last type satisfying `Criteria`.
412
412
  - `Head<A>`: gets the first entry in the array.
413
413
  - `IntersectOfProps<A, K>`: gets the intersect of `A[K]` types (deprecate `MapToProp`)
414
414
  - `IsArray<T>`: `logical` predicate for `Array`.
415
- - `literalArray(...entries)`: return an array those items are restricted to the provided literals.
415
+ - `literalArray(...entries)`: return an array whose items are restricted to the provided literals.
416
416
  - `PadLeft<A, Total, PadWith>`: pads `A` with `PadWith` if the length of `A` is less than `L`.
417
417
  - `reduceWhile()`: `reduce()` with predicate for early termination. \
418
418
  A simple version of the same function in the `ramda` package.
@@ -426,6 +426,8 @@ type No = IsAny<1, 'yes', 'no'> // 'no'
426
426
 
427
427
  - `KeyTypes`: type of all keys.
428
428
  - `PrimitiveTypes`: all primitive types, including `Function`, `symbol`, and `bigint`.
429
+ - `ComposableTypes`: Types that can contain custom properties. i.e. `object`, `array`, `function`.
430
+ - `NonComposableTypes`: Types that cannot contain custom properties. i.e. not composable.
429
431
 
430
432
  ### JSON Support
431
433
 
@@ -467,9 +469,9 @@ JSONTypes.get<string>(someJson, 'a', 'b', 1, 'c') // miku
467
469
 
468
470
  ### Type manipulation
469
471
 
470
- - `ANotB<A, B>`: get object with properties in `A` and not in `B`, including properties with different value type.
472
+ - `ANotB<A, B>`: get object with properties in `A` and not in `B`, including properties with a different value type.
471
473
  - `BNotA<A, B>`: flip of `ANotB`
472
- - `as<T>(subject)`: assert `subject` as `T`. Avoid ASI issue such as `;(x as any).abc`
474
+ - `as<T>(subject)`: assert `subject` as `T`. Avoid ASI issues such as `;(x as any).abc`
473
475
  - `asAny(subject)`: assert `subject` as `any`. Avoid ASI issue such as `;(x as any).abc`
474
476
  - `Except<T, K>`: Deprecated. Same as `Omit<T, K>`.
475
477
  - `ExcludePropType<T, U>`: excludes type `U` from properties in `T`.
@@ -481,13 +483,13 @@ JSONTypes.get<string>(someJson, 'a', 'b', 1, 'c') // miku
481
483
  - `PartialExcept<T, U>`: Deprecated. Same as `PartialOmit<T, U>`.
482
484
  - `PartialOmit<T, U>`: makes the properties not specified in `U` becomes optional.
483
485
  - `PartialPick<T, U>`: makes the properties specified in `U` becomes optional.
484
- - `Pick<T, K>`: pick properties `K` from `T`. Works with union.
486
+ - `Pick<T, K>`: pick properties `K` from `T`. Works with unions.
485
487
  - `RecursivePartial<T>`: make type `T` optional recursively.
486
488
  - `RecursiveRequired<T>`: make type `T` required recursively.
487
489
  - `ReplaceProperty<T, K, V>`: replace property `K` in `T` with `V`.
488
490
  - `RequiredKeys<T>`: gets keys of required properties in `T`.
489
- - `RequiredPick<T, U>`: makes the properties specified in `U` becomes required.
490
- - `RequiredExcept<T, U>`: makes the properties not specified in `U` becomes required.
491
+ - `RequiredPick<T, U>`: makes the properties specified in `U` become required.
492
+ - `RequiredExcept<T, U>`: makes the properties not specified in `U` become required.
491
493
  - `RecursiveIntersect<T, U>`: intersect type `U` onto `T` recursively.
492
494
  - `ValueOf<T>`: type of the value of the properties of `T`.
493
495
  - `Widen<T>`: widen literal types.
@@ -513,7 +515,7 @@ They can be used to compose complex types.
513
515
  - `Xor<A, B>`: logical `XOR`.
514
516
  - `Not<X>`: logical `NOT`.
515
517
 
516
- Note that these types work correctly with `boolean` type.
518
+ Note that these types work correctly with the `boolean` type.
517
519
  e.g.:
518
520
 
519
521
  - `And<boolean, true> -> boolean`
@@ -533,8 +535,7 @@ So you may encounter some weird behavior if your logic is complex.
533
535
  #### Arithmetics
534
536
 
535
537
  - `Add<A, B, Fail=never>`: `A + B` for positive and whole numbers, `Fail` otherwise.
536
- - `Subtract<A, B, Fail=never>`: `A - B` for positive and whole numbers, `Fail` otherwise.\
537
- Negative result also returns `Fail`.
538
+ - `Subtract<A, B, Fail=never>`: `A - B` for positive and whole numbers, `Fail` otherwise.
538
539
  - `Increment<A, Fail=never>`: alias of `Add<A, 1, Fail>`.
539
540
  - `Decrement<A, Fail=never>`: alias of `Subtract<A, 1, Fail>`.
540
541
 
@@ -571,9 +572,9 @@ overrider(source, { foo: !!source.foo })
571
572
 
572
573
  ## Attribution
573
574
 
574
- Some of the code in this library are created by other people in the TypeScript community.
575
- I merely adding them in and may be making some adjustments.
576
- When ever possible, I add attribution to the person who created those code in the file.
575
+ Some of the code in this library is created by other people in the TypeScript community.
576
+ I merely adding them in and maybe making some adjustments.
577
+ Whenever possible, I add attribution to the person who created those **codes** in the file.
577
578
 
578
579
  ## Similar projects
579
580
 
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Types that can contain custom properties.
3
+ */
4
+ export declare type ComposableTypes = object | Function;
5
+ /**
6
+ * Types that cannot contain custom properties.
7
+ */
8
+ export declare type NonComposableTypes = boolean | number | string | symbol | bigint | undefined | null;
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ComposableTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ComposableTypes.js","sourceRoot":"","sources":["../src/ComposableTypes.ts"],"names":[],"mappings":""}
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { required, requiredDeep } from 'unpartial';
3
3
  export * from './array';
4
4
  export * from './assertion';
5
5
  export * from './class';
6
+ export * from './ComposableTypes';
6
7
  export * from './function';
7
8
  export * from './functional';
8
9
  export * from './JSONTypes';
package/lib/index.js CHANGED
@@ -32,6 +32,7 @@ Object.defineProperty(exports, "requiredDeep", { enumerable: true, get: function
32
32
  __exportStar(require("./array"), exports);
33
33
  __exportStar(require("./assertion"), exports);
34
34
  __exportStar(require("./class"), exports);
35
+ __exportStar(require("./ComposableTypes"), exports);
35
36
  __exportStar(require("./function"), exports);
36
37
  __exportStar(require("./functional"), exports);
37
38
  __exportStar(require("./JSONTypes"), exports);
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAgC;AAkBvB,sBAAK;AAAW,kBAAC;AAjB1B,uCAAkD;AAAzC,qGAAA,QAAQ,OAAA;AAAE,yGAAA,YAAY,OAAA;AAC/B,0CAAuB;AACvB,8CAA2B;AAC3B,0CAAuB;AACvB,6CAA0B;AAC1B,+CAA4B;AAC5B,8CAA2B;AAC3B,yCAAsB;AACtB,kDAA+B;AAC/B,2CAAwB;AACxB,+CAA4B;AAC5B,mDAAgC;AAChC,4CAAyB;AACzB,mDAAgC;AAChC,mDAAgC;AAChC,8CAA2B;AAC3B,0CAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6CAAgC;AAmBvB,sBAAK;AAAW,kBAAC;AAlB1B,uCAAkD;AAAzC,qGAAA,QAAQ,OAAA;AAAE,yGAAA,YAAY,OAAA;AAC/B,0CAAuB;AACvB,8CAA2B;AAC3B,0CAAuB;AACvB,oDAAiC;AACjC,6CAA0B;AAC1B,+CAA4B;AAC5B,8CAA2B;AAC3B,yCAAsB;AACtB,kDAA+B;AAC/B,2CAAwB;AACxB,+CAA4B;AAC5B,mDAAgC;AAChC,4CAAyB;AACzB,mDAAgC;AAChC,mDAAgC;AAChC,8CAA2B;AAC3B,0CAAuB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ComposableTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ComposableTypes.js","sourceRoot":"","sources":["../src/ComposableTypes.ts"],"names":[],"mappings":""}
package/libm/index.js CHANGED
@@ -3,6 +3,7 @@ export { required, requiredDeep } from 'unpartial';
3
3
  export * from './array';
4
4
  export * from './assertion';
5
5
  export * from './class';
6
+ export * from './ComposableTypes';
6
7
  export * from './function';
7
8
  export * from './functional';
8
9
  export * from './JSONTypes';
package/libm/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAClD,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA;AACtB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAC5B,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,OAAO,EAAE,KAAK,EAAE,KAAK,IAAI,CAAC,EAAE,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AAClD,cAAc,SAAS,CAAA;AACvB,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,QAAQ,CAAA;AACtB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,UAAU,CAAA;AACxB,cAAc,cAAc,CAAA;AAC5B,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,OAAO,EAAE,KAAK,EAAE,KAAK,IAAI,CAAC,EAAE,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-plus",
3
- "version": "3.13.1",
3
+ "version": "3.14.0",
4
4
  "description": "Provides additional types for `typescript`.",
5
5
  "homepage": "https://github.com/unional/type-plus",
6
6
  "bugs": {
@@ -36,7 +36,6 @@
36
36
  "dependency-check": "dependency-check . --unused --no-dev -i ts-toolbelt -i typescript && dependency-check . --missing --no-dev",
37
37
  "lint": "eslint --ext=ts,js .",
38
38
  "nuke": "yarn clean && rimraf node_modules",
39
- "semantic-release": "semantic-release",
40
39
  "test": "tsc -p tsconfig.json && jest",
41
40
  "verify": "yarn lint && yarn build && yarn dc && npx size-limit && yarn coverage",
42
41
  "verify:ci": "yarn lint && yarn build && yarn dc && npx size-limit && yarn coverage --maxWorkers=2",
@@ -0,0 +1,29 @@
1
+ import { assertType, canAssign, ComposableTypes, NonComposableTypes } from '.'
2
+
3
+ test('ComposableTypes includes object, array, and function', () => {
4
+ assertType<ComposableTypes>({})
5
+ assertType<ComposableTypes>([])
6
+ // function is composable because you can do
7
+ // `Object.assign(fn, { ... })
8
+ assertType<ComposableTypes>(() => { })
9
+
10
+ assertType.isTrue(canAssign<ComposableTypes>(false)(null))
11
+ assertType.isTrue(canAssign<ComposableTypes>(false)(undefined))
12
+ assertType.isTrue(canAssign<ComposableTypes>(false)(1))
13
+ assertType.isTrue(canAssign<ComposableTypes>(false)(true))
14
+ assertType.isTrue(canAssign<ComposableTypes>(false)(''))
15
+ assertType.isTrue(canAssign<ComposableTypes>(false)(Symbol()))
16
+ })
17
+
18
+ test('NonComposableType excludes object, array, and function', () => {
19
+ assertType<NonComposableTypes>(null)
20
+ assertType<NonComposableTypes>(undefined)
21
+ assertType<NonComposableTypes>(true)
22
+ assertType<NonComposableTypes>(1)
23
+ assertType<NonComposableTypes>('')
24
+ assertType<NonComposableTypes>(Symbol())
25
+
26
+ assertType.isTrue(canAssign<NonComposableTypes>(false)({}))
27
+ assertType.isTrue(canAssign<NonComposableTypes>(false)([]))
28
+ assertType.isTrue(canAssign<NonComposableTypes>(false)(() => { }))
29
+ })
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Types that can contain custom properties.
3
+ */
4
+ // eslint-disable-next-line @typescript-eslint/ban-types
5
+ export type ComposableTypes = object | Function
6
+
7
+ /**
8
+ * Types that cannot contain custom properties.
9
+ */
10
+ export type NonComposableTypes = boolean | number | string | symbol | bigint | undefined | null
@@ -38,6 +38,5 @@ test('Just<number can assign to Maybe<number>', () => {
38
38
  test('Just<string> is not assignable to Maybe<number>', () => {
39
39
  assertType.isFalse(false as CanAssign<Just<'abc'>, Maybe<number>>)
40
40
 
41
- // this is failing, which is wrong
42
- // assertType.isFalse(canAssign<Maybe<number>>()(just('abc')))
41
+ assertType.isTrue(canAssign<Maybe<number>>(false)(just('abc')))
43
42
  })
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export { required, requiredDeep } from 'unpartial'
3
3
  export * from './array'
4
4
  export * from './assertion'
5
5
  export * from './class'
6
+ export * from './ComposableTypes'
6
7
  export * from './function'
7
8
  export * from './functional'
8
9
  export * from './JSONTypes'