diff --git a/src/struct.ts b/src/struct.ts index 36f1f288..527fa683 100644 --- a/src/struct.ts +++ b/src/struct.ts @@ -231,8 +231,7 @@ export type Infer> = T['TYPE'] * A type utility to extract the type from a `Struct` class before coercion */ -export type InferUncoerced> = - T['TYPE'] | T['UNCOERCED_TYPE'] +export type InferUncoerced> = T['UNCOERCED_TYPE'] /** * A type utility to describe that a struct represents a TypeScript type. diff --git a/src/structs/coercions.ts b/src/structs/coercions.ts index 6866a38f..a2ef187d 100644 --- a/src/structs/coercions.ts +++ b/src/structs/coercions.ts @@ -13,11 +13,11 @@ import { string, unknown } from './types' * take effect! Using simply `assert()` or `is()` will not use coercion. */ -export function coerce( - struct: Struct, - condition: Struct, +export function coerce( + struct: Struct, + condition: Struct, coercer: Coercer -): Struct { +): Struct { return new Struct({ ...struct, coercer: (value, ctx) => { @@ -35,14 +35,14 @@ export function coerce( * take effect! Using simply `assert()` or `is()` will not use coercion. */ -export function defaulted( - struct: Struct, +export function defaulted( + struct: Struct, fallback: any, options: { strict?: boolean } = {} -): Struct { - return coerce(struct, unknown(), (x) => { +): Struct | C> { + return coerce(struct, unknown() as Struct>, (x) => { const f = typeof fallback === 'function' ? fallback() : fallback if (x === undefined) { @@ -76,6 +76,6 @@ export function defaulted( * take effect! Using simply `assert()` or `is()` will not use coercion. */ -export function trimmed(struct: Struct): Struct { +export function trimmed(struct: Struct): Struct { return coerce(struct, string(), (x) => x.trim()) }