ts-runtime-typecheck
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -13,12 +13,14 @@ export * from './type-cast/as-json'; | ||
export * from './type-coerce/make-strict-partial'; | ||
export { Index, Indexable } from './Index.type'; | ||
export { JSONValue, JSONArray, JSONObject } from './JSONValue.type'; | ||
export { Dictionary } from './Dictionary.type'; | ||
export { Optional } from './Optional.type'; | ||
export { Nullish } from './Nullish.type'; | ||
export { UnknownFunction, UnknownAsyncFunction } from './UnknownFunction.type'; | ||
export { InterfacePattern } from './InterfacePattern.type'; | ||
export { TypeCheck } from './TypeCheck.type'; | ||
export { StrictPartial } from './StrictPartial.type'; | ||
export { StrictRequired } from './StrictRequired.type'; | ||
export { FuzzyPartial } from './FuzzyPartial.type'; | ||
export * from './type-assert/assert-defined'; | ||
export { TypeAssertion } from './TypeAssertion'; | ||
export type { Index, Indexable } from './Index.type'; | ||
export type { JSONValue, JSONArray, JSONObject } from './JSONValue.type'; | ||
export type { Dictionary } from './Dictionary.type'; | ||
export type { Optional } from './Optional.type'; | ||
export type { Nullish } from './Nullish.type'; | ||
export type { UnknownFunction, UnknownAsyncFunction } from './UnknownFunction.type'; | ||
export type { InterfacePattern } from './InterfacePattern.type'; | ||
export type { TypeCheck } from './TypeCheck.type'; | ||
export type { StrictPartial } from './StrictPartial.type'; | ||
export type { StrictRequired } from './StrictRequired.type'; | ||
export type { FuzzyPartial } from './FuzzyPartial.type'; |
@@ -17,2 +17,4 @@ 'use strict'; | ||
var makeStrictPartial = require('./type-coerce/make-strict-partial.js'); | ||
var TypeAssertion = require('./TypeAssertion.js'); | ||
var assertDefined = require('./type-assert/assert-defined.js'); | ||
@@ -89,1 +91,3 @@ | ||
exports.makeStrictPartial = makeStrictPartial.makeStrictPartial; | ||
exports.TypeAssertion = TypeAssertion.TypeAssertion; | ||
exports.assertDefined = assertDefined.assertDefined; |
@@ -13,12 +13,14 @@ export * from './type-cast/as-json'; | ||
export * from './type-coerce/make-strict-partial'; | ||
export { Index, Indexable } from './Index.type'; | ||
export { JSONValue, JSONArray, JSONObject } from './JSONValue.type'; | ||
export { Dictionary } from './Dictionary.type'; | ||
export { Optional } from './Optional.type'; | ||
export { Nullish } from './Nullish.type'; | ||
export { UnknownFunction, UnknownAsyncFunction } from './UnknownFunction.type'; | ||
export { InterfacePattern } from './InterfacePattern.type'; | ||
export { TypeCheck } from './TypeCheck.type'; | ||
export { StrictPartial } from './StrictPartial.type'; | ||
export { StrictRequired } from './StrictRequired.type'; | ||
export { FuzzyPartial } from './FuzzyPartial.type'; | ||
export * from './type-assert/assert-defined'; | ||
export { TypeAssertion } from './TypeAssertion'; | ||
export type { Index, Indexable } from './Index.type'; | ||
export type { JSONValue, JSONArray, JSONObject } from './JSONValue.type'; | ||
export type { Dictionary } from './Dictionary.type'; | ||
export type { Optional } from './Optional.type'; | ||
export type { Nullish } from './Nullish.type'; | ||
export type { UnknownFunction, UnknownAsyncFunction } from './UnknownFunction.type'; | ||
export type { InterfacePattern } from './InterfacePattern.type'; | ||
export type { TypeCheck } from './TypeCheck.type'; | ||
export type { StrictPartial } from './StrictPartial.type'; | ||
export type { StrictRequired } from './StrictRequired.type'; | ||
export type { FuzzyPartial } from './FuzzyPartial.type'; |
{ | ||
"name": "ts-runtime-typecheck", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "A collection of common types for TypeScript along with dynamic type cast methods.", | ||
@@ -15,3 +15,3 @@ "main": "cjs/index.js", | ||
"type": "git", | ||
"url": "git@github.com:nanoporetech/ts-extended-types.git" | ||
"url": "git@github.com:nanoporetech/ts-runtime-typecheck.git" | ||
}, | ||
@@ -18,0 +18,0 @@ "author": "Iain Shorter", |
@@ -28,2 +28,3 @@ # ts-runtime-typecheck | ||
- [Type Coerce](#type-coerce) | ||
- [Type Asserts](#type-asserts) | ||
- [JSON Types](#json-types) | ||
@@ -41,2 +42,3 @@ - [Ensuring an optional value is defined](#ensuring-an-optional-value-is-defined) | ||
- [Reference: Type Coerce](#reference-type-coerce) | ||
- [Reference: Type Assert](#reference-type-assert) | ||
- [Reference: Types](#reference-types) | ||
@@ -51,2 +53,3 @@ - [Changelog](#changelog) | ||
- [v2.1.1](#211) | ||
- [v2.2.0](#220) | ||
@@ -160,2 +163,26 @@ ## Type Casts | ||
## Type Asserts | ||
**Type Assert** functions accept an `unknown` value and throw if the value does not meet the type requirement, they do not return a value. While this may seem very similar to [Type Casts](#type-casts) they are capable of providing a hint to the TypeScript compiler without needing to reassign the value. As such they are very helpful for validating function arguments before using them. | ||
Each type assert takes an optional second argument that is a label for the passed value, this will be included in the thrown `TypeAssertion` error if the value does not meet the type requirement, making it easier to isolate the type violation. | ||
At the moment only 1 `TypeAssert` function exists which is `assertDefined`. It works in a similar way to `asDefined`, in that it accepts a generic type for its input, which is expected to be a union including null and/or undefined and asserts that the value is NonNullable. This is especially helpful for dealing with optional function arguments, if you want to ensure they exist under a certain situation you can just call `assertDefined`. | ||
```typescript | ||
import { assertDefined } from 'ts-runtime-typecheck'; | ||
function main (meaningOfLife: Optional<number>) { | ||
meaningOfLife // number | null | undefined | ||
assertDefined(meaningOfLife, 'Meaning of Life'); | ||
meaningOfLife // number | ||
return 'but what is the question?'; | ||
} | ||
main(42); // 'but what is the question?' | ||
main(); // TypeAssertion: Meaning of Life is not defined | ||
``` | ||
--- | ||
## JSON Types | ||
@@ -663,2 +690,8 @@ | ||
### Reference: Type Assert | ||
- ### assertDefined | ||
Assert value of type [`Type | Nullish`](#nullish) is `Type`, where `Type` is a generic parameter. Accepts an optional name for the value that is included in the error if the value is nullish. | ||
### Reference: Types | ||
@@ -726,2 +759,6 @@ | ||
- ### TypeAssertion | ||
A custom error with the name `TypeAssertion`. This type is exported as a value so that Errors of this type can be isolated from other errors using instance checks. It is possible to use the constructor to create and throw your own Errors if you wish, **but this may change in future**. | ||
## Changelog | ||
@@ -780,1 +817,6 @@ | ||
- Fix: incorrect constraint on `makeStrictPartial` prevented passing in non-indexable instances. | ||
### 2.2.0 | ||
- Add: `assertDefined` throws if the passed value is `Nullish`. | ||
- Add: `TypeAssertion` error class thrown by TypeAsserts. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
108439
100
1560
816