Comparing version 0.20.0 to 0.21.0
@@ -29,3 +29,3 @@ import { Predicate } from './predicates/predicate'; | ||
*/ | ||
<T>(value: T, predicate: BasePredicate<T>): void; | ||
<T>(value: unknown, predicate: BasePredicate<T>): asserts value is T; | ||
/** | ||
@@ -38,3 +38,3 @@ Test if `value` matches the provided `predicate`. Throws an `ArgumentError` with the specified `label` if the test fails. | ||
*/ | ||
<T>(value: T, label: string, predicate: BasePredicate<T>): void; | ||
<T>(value: unknown, label: string, predicate: BasePredicate<T>): asserts value is T; | ||
} | ||
@@ -53,6 +53,6 @@ /** | ||
} | ||
declare const _default: Ow; | ||
export default _default; | ||
declare const _ow: Ow; | ||
export default _ow; | ||
export { BasePredicate, Predicate }; | ||
export { StringPredicate, NumberPredicate, BooleanPredicate, ArrayPredicate, ObjectPredicate, DatePredicate, ErrorPredicate, MapPredicate, WeakMapPredicate, SetPredicate, WeakSetPredicate, TypedArrayPredicate, ArrayBufferPredicate, DataViewPredicate, AnyPredicate, Shape } from './predicates'; | ||
export { ArgumentError } from './argument-error'; |
@@ -47,3 +47,7 @@ "use strict"; | ||
}); | ||
exports.default = predicates_1.default(modifiers_1.default(ow)); | ||
// Can't use `export default predicates(modifiers(ow)) as Ow` because the variable needs a type annotation to avoid a compiler error when used: | ||
// Assertions require every name in the call target to be declared with an explicit type annotation.ts(2775) | ||
// See https://github.com/microsoft/TypeScript/issues/36931 for more details. | ||
const _ow = predicates_1.default(modifiers_1.default(ow)); | ||
exports.default = _ow; | ||
var predicates_2 = require("./predicates"); | ||
@@ -50,0 +54,0 @@ Object.defineProperty(exports, "StringPredicate", { enumerable: true, get: function () { return predicates_2.StringPredicate; } }); |
@@ -0,2 +1,4 @@ | ||
import { BasePredicate } from '.'; | ||
import { Predicates } from './predicates'; | ||
declare type Optionalify<P> = P extends BasePredicate<infer X> ? P & BasePredicate<X | undefined> : P; | ||
export interface Modifiers { | ||
@@ -6,5 +8,7 @@ /** | ||
*/ | ||
readonly optional: Predicates; | ||
readonly optional: { | ||
[K in keyof Predicates]: Optionalify<Predicates[K]>; | ||
}; | ||
} | ||
declare const _default: <T>(object: T) => T & Modifiers; | ||
export default _default; |
@@ -74,3 +74,3 @@ import { BasePredicate } from './base-predicate'; | ||
*/ | ||
ofType<P extends BasePredicate<T>>(predicate: P): this; | ||
ofType<U extends T>(predicate: BasePredicate<U>): ArrayPredicate<U>; | ||
} |
@@ -134,2 +134,3 @@ "use strict"; | ||
let error; | ||
// TODO [typescript@>=5] If higher-kinded types are supported natively by typescript, refactor `addValidator` to use them to avoid the usage of `any`. Otherwise, bump or remove this TODO. | ||
return this.addValidator({ | ||
@@ -136,0 +137,0 @@ message: (_, label) => `(${label}) ${error}`, |
@@ -1,6 +0,6 @@ | ||
import { Shape } from '../utils/match-shape'; | ||
import { Shape, TypeOfShape } from '../utils/match-shape'; | ||
import { Predicate, PredicateOptions } from './predicate'; | ||
import { BasePredicate } from './base-predicate'; | ||
export { Shape }; | ||
export declare class ObjectPredicate extends Predicate<object> { | ||
export declare class ObjectPredicate<T extends object = object> extends Predicate<T> { | ||
/** | ||
@@ -79,3 +79,3 @@ @hidden | ||
*/ | ||
partialShape(shape: Shape): this; | ||
partialShape<S extends Shape = Shape>(shape: S): ObjectPredicate<TypeOfShape<S>>; | ||
/** | ||
@@ -97,3 +97,3 @@ Test an object to match the `shape` exactly. This means that will fail if it comes across unexpected properties. The shape comparison is deep. | ||
*/ | ||
exactShape(shape: Shape): this; | ||
exactShape<S extends Shape = Shape>(shape: S): ObjectPredicate<TypeOfShape<S>>; | ||
} |
@@ -165,2 +165,3 @@ "use strict"; | ||
exactShape(shape) { | ||
// TODO [typescript@>=5] If higher-kinded types are supported natively by typescript, refactor `addValidator` to use them to avoid the usage of `any`. Otherwise, bump or remove this TODO. | ||
return this.addValidator({ | ||
@@ -167,0 +168,0 @@ // TODO: Improve this when message handling becomes smarter |
@@ -62,3 +62,3 @@ import { BasePredicate, testSymbol } from './base-predicate'; | ||
*/ | ||
[testSymbol](value: T | undefined, main: Main, label: string | Function): asserts value; | ||
[testSymbol](value: T, main: Main, label: string | Function): asserts value is T; | ||
/** | ||
@@ -65,0 +65,0 @@ @hidden |
@@ -60,4 +60,3 @@ "use strict"; | ||
} | ||
const knownValue = value; | ||
const result = validator(knownValue); | ||
const result = validator(value); | ||
if (result === true) { | ||
@@ -74,3 +73,3 @@ continue; | ||
// TODO: Modify the stack output to show the original `ow()` call instead of this `throw` statement | ||
throw new argument_error_1.ArgumentError(message(knownValue, label2, result), main); | ||
throw new argument_error_1.ArgumentError(message(value, label2, result), main); | ||
} | ||
@@ -77,0 +76,0 @@ } |
@@ -6,2 +6,22 @@ import { BasePredicate } from '..'; | ||
/** | ||
Extracts a regular type from a shape definition. | ||
@example | ||
``` | ||
const myShape = { | ||
foo: ow.string, | ||
bar: { | ||
baz: ow.boolean | ||
} | ||
} | ||
type X = TypeOfShape<typeof myShape> // {foo: string; bar: {baz: boolean}} | ||
``` | ||
This is used in the `ow.object.partialShape(…)` and `ow.object.exactShape(…)` functions. | ||
*/ | ||
export declare type TypeOfShape<S extends BasePredicate | Shape> = S extends BasePredicate<infer X> ? X : S extends Shape ? { | ||
[K in keyof S]: TypeOfShape<S[K]>; | ||
} : never; | ||
/** | ||
Test if the `object` matches the `shape` partially. | ||
@@ -8,0 +28,0 @@ |
{ | ||
"name": "ow", | ||
"version": "0.20.0", | ||
"version": "0.21.0", | ||
"description": "Function argument validation for humans", | ||
@@ -59,11 +59,12 @@ "license": "MIT", | ||
"@types/lodash.isequal": "^4.5.5", | ||
"@types/node": "^14.14.10", | ||
"@types/node": "^14.14.14", | ||
"@types/vali-date": "^1.0.0", | ||
"ava": "^2.0.0", | ||
"del-cli": "^3.0.1", | ||
"expect-type": "^0.11.0", | ||
"nyc": "^15.1.0", | ||
"ts-node": "^9.0.0", | ||
"ts-node": "^9.1.1", | ||
"typedoc": "^0.19.2", | ||
"typescript": "~4.1.2", | ||
"xo": "^0.35.0" | ||
"typescript": "~4.1.3", | ||
"xo": "^0.36.1" | ||
}, | ||
@@ -70,0 +71,0 @@ "browser": { |
@@ -72,4 +72,14 @@ <p align="center"> | ||
Ow does not currently include TypeScript type guards, but we do [plan to include type assertions](https://github.com/sindresorhus/ow/issues/159). | ||
Ow includes TypeScript type guards, so using it will narrow the type of previously-unknown values. | ||
```ts | ||
function (input: unknown) { | ||
input.slice(0, 3) // Error, Property 'slice' does not exist on type 'unknown' | ||
ow(input, ow.string) | ||
input.slice(0, 3) // OK | ||
} | ||
``` | ||
### ow(value, predicate) | ||
@@ -76,0 +86,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
128690
3138
298
0
12