@wopjs/cast
Advanced tools
+7
-7
| type _ = undefined; | ||
| /** Map `never` to a different type */ | ||
| type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1]; | ||
| /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */ | ||
| type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1]; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
@@ -12,3 +12,3 @@ declare const isDefined: <T>(x: T | undefined) => x is T; | ||
| type Falsy = false | null | undefined | 0 | ""; | ||
| type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>; | ||
| type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `false`. */ | ||
@@ -41,3 +41,3 @@ declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>; | ||
| declare const toNonEmptyString: (x: unknown) => string | _; | ||
| type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>; | ||
| type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>; | ||
| declare const isArray: <T>(x: T) => x is ExtractArray<T>; | ||
@@ -52,3 +52,3 @@ /** Returns `x` if `x` is an array. */ | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _; | ||
| type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>; | ||
| type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>; | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
@@ -63,3 +63,3 @@ declare const isObject: <T>(x: T) => x is ExtractObject<T>; | ||
| } | ||
| type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */ | ||
@@ -101,2 +101,2 @@ declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type MapNeverTo, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; |
+7
-7
| type _ = undefined; | ||
| /** Map `never` to a different type */ | ||
| type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1]; | ||
| /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */ | ||
| type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1]; | ||
| /** Returns `true` if `x` is not `undefined`. */ | ||
@@ -12,3 +12,3 @@ declare const isDefined: <T>(x: T | undefined) => x is T; | ||
| type Falsy = false | null | undefined | 0 | ""; | ||
| type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>; | ||
| type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>; | ||
| /** Returns `true` if `Boolean(x)` is `false`. */ | ||
@@ -41,3 +41,3 @@ declare const isFalsy: <T>(x: T) => x is ExtractFalsy<T>; | ||
| declare const toNonEmptyString: (x: unknown) => string | _; | ||
| type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>; | ||
| type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>; | ||
| declare const isArray: <T>(x: T) => x is ExtractArray<T>; | ||
@@ -52,3 +52,3 @@ /** Returns `x` if `x` is an array. */ | ||
| declare const toNonEmptyArray: <T>(x: T) => ExtractArray<T> | _; | ||
| type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>; | ||
| type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>; | ||
| /** Returns `true` if `x` is an object (including array) and not null. */ | ||
@@ -63,3 +63,3 @@ declare const isObject: <T>(x: T) => x is ExtractObject<T>; | ||
| } | ||
| type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */ | ||
@@ -101,2 +101,2 @@ declare const isPlainObject: <T>(x: T) => x is ExtractPlainObject<T>; | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type MapNeverTo, type PlainObject, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; | ||
| export { type ExtractArray, type ExtractFalsy, type ExtractObject, type ExtractPlainObject, type Falsy, type PlainObject, type SetDefaultType, type _, asArray, asNumber, asObject, asPlainObject, asString, asTrue, isArray, isBoolean, isDefined, isFalsy, isNonEmptyArray, isNonEmptyJSONObject, isNonEmptyPlainObject, isNonEmptyString, isNumber, isObject, isPlainObject, isString, isTrue, isTruthy, noop, print, returnsEmptyString, returnsFalse, returnsNull, returnsTrue, returnsUndefined, toArray, toBoolean, toFalsy, toNonEmptyArray, toNonEmptyJSONObject, toNonEmptyPlainObject, toNonEmptyString, toNumber, toObject, toPlainObject, toPlainObjectOf, toPlainObjectOfTrue, toString, toTrue, toTruthy }; |
+1
-1
| { | ||
| "name": "@wopjs/cast", | ||
| "version": "0.1.8", | ||
| "version": "0.1.9", | ||
| "description": "Type-safe utilities for filtering and coercing unknown values in TypeScript.", | ||
@@ -5,0 +5,0 @@ "repository": "wopjs/cast", |
+21
-0
@@ -307,2 +307,12 @@ import type { PlainObject } from "."; | ||
| } | ||
| { | ||
| // Type narrowing - generic | ||
| const _fn = <T>(x: T): void => { | ||
| if (isArray(x)) { | ||
| const y: readonly any[] = x; | ||
| expect(Array.isArray(y)).toBe(true); | ||
| } | ||
| }; | ||
| } | ||
| }); | ||
@@ -376,3 +386,12 @@ | ||
| expect(result).toBe(undefined); | ||
| toArray(arr)?.map(x => x); | ||
| } | ||
| { | ||
| // Type narrowing - any returns any[] | ||
| const arr = castType<any>("hello"); | ||
| const result: unknown[] | undefined = toArray(arr); | ||
| expect(result).toBe(undefined); | ||
| toArray(arr)?.map(x => x); | ||
| } | ||
| }); | ||
@@ -512,2 +531,3 @@ | ||
| expect(result).toEqual(["a", "b"]); | ||
| asArray(arr).map(x => x); | ||
| } | ||
@@ -520,2 +540,3 @@ | ||
| expect(result).toEqual([]); | ||
| asArray(arr).map(x => x); | ||
| } | ||
@@ -522,0 +543,0 @@ }); |
+6
-6
| const _ = undefined; | ||
| export type _ = undefined; | ||
| /** Map `never` to a different type */ | ||
| export type MapNeverTo<T, U> = [T, U][T extends any ? 0 : 1]; | ||
| /** Returns `U` if `T` is `never` or `any`, otherwise returns `T`. */ | ||
| export type SetDefaultType<T, U> = [T, U][T extends any ? (0 extends 1 & T ? 1 : 0) : 1]; | ||
@@ -20,3 +20,3 @@ /** Returns `true` if `x` is not `undefined`. */ | ||
| export type ExtractFalsy<T> = MapNeverTo<Extract<T, Falsy>, Falsy>; | ||
| export type ExtractFalsy<T> = SetDefaultType<Extract<T, Falsy>, Falsy>; | ||
@@ -64,3 +64,3 @@ /** Returns `true` if `Boolean(x)` is `false`. */ | ||
| export type ExtractArray<T> = MapNeverTo<Extract<T, readonly unknown[]>, unknown[]>; | ||
| export type ExtractArray<T> = SetDefaultType<Extract<T, readonly unknown[]>, unknown[]>; | ||
@@ -81,3 +81,3 @@ export const isArray = Array.isArray as <T>(x: T) => x is ExtractArray<T>; | ||
| export type ExtractObject<T> = MapNeverTo<Extract<T, object>, object>; | ||
| export type ExtractObject<T> = SetDefaultType<Extract<T, object>, object>; | ||
@@ -97,3 +97,3 @@ /** Returns `true` if `x` is an object (including array) and not null. */ | ||
| export type ExtractPlainObject<T> = MapNeverTo<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
| export type ExtractPlainObject<T> = SetDefaultType<Exclude<Extract<T, object>, readonly unknown[]>, PlainObject>; | ||
@@ -100,0 +100,0 @@ /** Returns `true` if `x` is a plain object (shallow test), not `null` or array. */ |
73944
1.06%1347
1.43%