Skip to content

Commit fb847ca

Browse files
committed
fix coercions
- improve `InferUncoerced`: the logic to decide what the uncoerced type is happens in the `coerce` function instead, where it should be - improve the uncoerced type parameter in the return type of `defaulted`
1 parent b10c37c commit fb847ca

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/struct.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ export type Infer<T extends Struct<any, any, any>> = T['TYPE']
231231
* A type utility to extract the type from a `Struct` class before coercion
232232
*/
233233

234-
export type InferUncoerced<T extends Struct<any, any, any>> =
235-
T['TYPE'] | T['UNCOERCED_TYPE']
234+
export type InferUncoerced<T extends Struct<any, any, any>> = T['UNCOERCED_TYPE']
236235

237236
/**
238237
* A type utility to describe that a struct represents a TypeScript type.

src/structs/coercions.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import { string, unknown } from './types'
1313
* take effect! Using simply `assert()` or `is()` will not use coercion.
1414
*/
1515

16-
export function coerce<T, S, C>(
17-
struct: Struct<T, S>,
18-
condition: Struct<C, any>,
16+
export function coerce<T, S, C, UC>(
17+
struct: Struct<T, S, UC>,
18+
condition: Struct<C, any, any>,
1919
coercer: Coercer<C>
20-
): Struct<T, S, C> {
20+
): Struct<T, S, UC | C | T> {
2121
return new Struct({
2222
...struct,
2323
coercer: (value, ctx) => {
@@ -35,14 +35,14 @@ export function coerce<T, S, C>(
3535
* take effect! Using simply `assert()` or `is()` will not use coercion.
3636
*/
3737

38-
export function defaulted<T, S>(
39-
struct: Struct<T, S>,
38+
export function defaulted<T, S, C>(
39+
struct: Struct<T, S, C>,
4040
fallback: any,
4141
options: {
4242
strict?: boolean
4343
} = {}
44-
): Struct<T, S, unknown> {
45-
return coerce(struct, unknown(), (x) => {
44+
): Struct<T, S, undefined | Partial<T> | C> {
45+
return coerce(struct, unknown() as Struct<undefined | Partial<T>>, (x) => {
4646
const f = typeof fallback === 'function' ? fallback() : fallback
4747

4848
if (x === undefined) {
@@ -76,6 +76,6 @@ export function defaulted<T, S>(
7676
* take effect! Using simply `assert()` or `is()` will not use coercion.
7777
*/
7878

79-
export function trimmed<T, S>(struct: Struct<T, S>): Struct<T, S, string> {
79+
export function trimmed<T, S>(struct: Struct<T, S>): Struct<T, S, string| T> {
8080
return coerce(struct, string(), (x) => x.trim())
8181
}

0 commit comments

Comments
 (0)