@paulpopat/safe-type
Advanced tools
Comparing version 2.1.3 to 2.1.4
@@ -23,7 +23,7 @@ export declare type IsType<T> = T extends (arg: any) => arg is infer T ? T : never; | ||
[K in keyof T]: Checker<T[K]>; | ||
}): (arg: any) => arg is T[number]; | ||
declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; | ||
}): (arg: any, strict?: boolean) => arg is T[number]; | ||
declare type UnionToIntersection<T extends any[]> = T extends [infer F, ...infer R] ? F & UnionToIntersection<R> : unknown; | ||
export declare function IsIntersection<T extends any[]>(...checkers: { | ||
[K in keyof T]: Checker<T[K]>; | ||
}): (arg: any) => arg is UnionToIntersection<T[number]>; | ||
}): (arg: any) => arg is UnionToIntersection<T>; | ||
export declare function IsObject<T extends CheckerObject>(checker: T): ObjectChecker<T>; | ||
@@ -30,0 +30,0 @@ export declare function IsDictionary<T>(c: Checker<T>): Checker<{ |
@@ -70,4 +70,5 @@ "use strict"; | ||
} | ||
return function (arg) { | ||
return checkers.filter(function (c) { return c(arg, true); }).length > 0; | ||
return function (arg, strict) { | ||
if (strict === void 0) { strict = true; } | ||
return checkers.filter(function (c) { return c(arg, strict); }).length > 0; | ||
}; | ||
@@ -74,0 +75,0 @@ } |
{ | ||
"name": "@paulpopat/safe-type", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"description": "Typesafety with strong inference", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -164,1 +164,20 @@ import { | ||
}); | ||
it("Matches on an intersection of unions", () => { | ||
const checker = IsIntersection( | ||
IsObject({ | ||
thing: IsString, | ||
}), | ||
IsUnion( | ||
IsObject({ test: IsString }), | ||
IsIntersection( | ||
IsObject({ test: IsString }), | ||
IsObject({ other: IsString }) | ||
) | ||
) | ||
); | ||
expect(checker({ thing: "test", test: "other", other: "test" })).toBe(true); | ||
expect(checker({ thing: "test", test: "other" })).toBe(true); | ||
expect(checker({ thing: "test" })).toBe(false); | ||
expect(checker({ test: "other" })).toBe(false); | ||
}); |
@@ -74,11 +74,9 @@ export type IsType<T> = T extends (arg: any) => arg is infer T ? T : never; | ||
) { | ||
return (arg: any): arg is T[number] => | ||
checkers.filter((c) => c(arg, true)).length > 0; | ||
return (arg: any, strict: boolean = true): arg is T[number] => | ||
checkers.filter((c) => c(arg, strict)).length > 0; | ||
} | ||
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ( | ||
k: infer I | ||
) => void | ||
? I | ||
: never; | ||
type UnionToIntersection<T extends any[]> = T extends [infer F, ...infer R] | ||
? F & UnionToIntersection<R> | ||
: unknown; | ||
@@ -88,3 +86,3 @@ export function IsIntersection<T extends any[]>( | ||
) { | ||
return (arg: any): arg is UnionToIntersection<T[number]> => | ||
return (arg: any): arg is UnionToIntersection<T> => | ||
checkers.filter((c) => c(arg, false)).length === checkers.length; | ||
@@ -91,0 +89,0 @@ } |
21182
573