Skip to content

Commit b10c37c

Browse files
committed
linting
1 parent 702d1e9 commit b10c37c

File tree

5 files changed

+61
-30
lines changed

5 files changed

+61
-30
lines changed

src/struct.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ export function mask<T, S, C>(
170170
* Check if a value passes a struct.
171171
*/
172172

173-
export function is<T, S, C>(value: unknown, struct: Struct<T, S, C>): value is T {
173+
export function is<T, S, C>(
174+
value: unknown,
175+
struct: Struct<T, S, C>
176+
): value is T {
174177
const result = validate(value, struct)
175178
return !result[0]
176179
}
@@ -228,8 +231,8 @@ export type Infer<T extends Struct<any, any, any>> = T['TYPE']
228231
* A type utility to extract the type from a `Struct` class before coercion
229232
*/
230233

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

234237
/**
235238
* A type utility to describe that a struct represents a TypeScript type.

src/structs/coercions.ts

Lines changed: 6 additions & 6 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, CT>(
17-
struct: Struct<T, S, CT>,
16+
export function coerce<T, S, C>(
17+
struct: Struct<T, S>,
1818
condition: Struct<C, any>,
1919
coercer: Coercer<C>
20-
): Struct<T, S, CT> {
20+
): Struct<T, S, C> {
2121
return new Struct({
2222
...struct,
2323
coercer: (value, ctx) => {
@@ -35,8 +35,8 @@ export function coerce<T, S, C, CT>(
3535
* take effect! Using simply `assert()` or `is()` will not use coercion.
3636
*/
3737

38-
export function defaulted<T, S, C>(
39-
struct: Struct<T, S, C>,
38+
export function defaulted<T, S>(
39+
struct: Struct<T, S>,
4040
fallback: any,
4141
options: {
4242
strict?: boolean
@@ -76,6 +76,6 @@ export function defaulted<T, S, C>(
7676
* take effect! Using simply `assert()` or `is()` will not use coercion.
7777
*/
7878

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

src/structs/types.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export function any(): Struct<any, null> {
2929
* and it is preferred to using `array(any())`.
3030
*/
3131

32-
export function array<T extends Struct<any>>(Element: T): Struct<Infer<T>[], T, InferUncoerced<T>[]>
32+
export function array<T extends Struct<any>>(
33+
Element: T
34+
): Struct<Infer<T>[], T, InferUncoerced<T>[]>
3335
export function array(): Struct<unknown[], undefined>
3436
export function array<T extends Struct<any>>(Element?: T): any {
3537
return new Struct({
@@ -172,7 +174,11 @@ export function integer(): Struct<number, null> {
172174

173175
export function intersection<A extends AnyStruct, B extends AnyStruct[]>(
174176
Structs: [A, ...B]
175-
): Struct<Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>, null, InferUncoerced<A> & UnionToIntersection<InferStructTupleUncoerced<B>[number]>> {
177+
): Struct<
178+
Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>,
179+
null,
180+
InferUncoerced<A> & UnionToIntersection<InferStructTupleUncoerced<B>[number]>
181+
> {
176182
return new Struct({
177183
type: 'intersection',
178184
schema: null,
@@ -264,7 +270,9 @@ export function never(): Struct<never, null> {
264270
* Augment an existing struct to allow `null` values.
265271
*/
266272

267-
export function nullable<T, S, C>(struct: Struct<T, S, C>): Struct<T | null, S> {
273+
export function nullable<T, S, C>(
274+
struct: Struct<T, S, C>
275+
): Struct<T | null, S> {
268276
return new Struct({
269277
...struct,
270278
validator: (value, ctx) => value === null || struct.validator(value, ctx),
@@ -434,7 +442,11 @@ export function string(): Struct<string, null> {
434442

435443
export function tuple<A extends AnyStruct, B extends AnyStruct[]>(
436444
Structs: [A, ...B]
437-
): Struct<[Infer<A>, ...InferStructTuple<B>], null, [InferUncoerced<A>, ...InferStructTupleUncoerced<B>]> {
445+
): Struct<
446+
[Infer<A>, ...InferStructTuple<B>],
447+
null,
448+
[InferUncoerced<A>, ...InferStructTupleUncoerced<B>]
449+
> {
438450
const Never = never()
439451

440452
return new Struct({
@@ -496,7 +508,11 @@ export function type<S extends ObjectSchema>(
496508

497509
export function union<A extends AnyStruct, B extends AnyStruct[]>(
498510
Structs: [A, ...B]
499-
): Struct<Infer<A> | InferStructTuple<B>[number], null, InferUncoerced<A> | InferStructTupleUncoerced<B>[number]> {
511+
): Struct<
512+
Infer<A> | InferStructTuple<B>[number],
513+
null,
514+
InferUncoerced<A> | InferStructTupleUncoerced<B>[number]
515+
> {
500516
const description = Structs.map((s) => s.type).join(' | ')
501517
return new Struct({
502518
type: 'union',

src/structs/utilities.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Struct, Context, Validator } from '../struct'
22
import { object, optional, type } from './types'
3-
import { ObjectSchema, Assign, ObjectType, PartialObjectSchema, ObjectTypeUncoerced } from '../utils'
3+
import {
4+
ObjectSchema,
5+
Assign,
6+
ObjectType,
7+
PartialObjectSchema,
8+
ObjectTypeUncoerced,
9+
} from '../utils'
410

511
/**
612
* Create a new struct that combines the properties properties from multiple

src/utils.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Struct, Infer, Result, Context, Describe, InferUncoerced } from './struct'
1+
import {
2+
Struct,
3+
Infer,
4+
Result,
5+
Context,
6+
Describe,
7+
InferUncoerced,
8+
} from './struct'
29
import { Failure } from './error'
310

411
/**
@@ -299,7 +306,6 @@ export type ObjectTypeUncoerced<S extends ObjectSchema> = Simplify<
299306
Optionalize<{ [K in keyof S]: InferUncoerced<S[K]> }>
300307
>
301308

302-
303309
/**
304310
* Omit properties from a type that extend from a specific type.
305311
*/
@@ -424,26 +430,26 @@ type _InferTuple<
424430
? Accumulated
425431
: _InferTuple<Tuple, Length, [...Accumulated, Infer<Tuple[Index]>]>
426432

427-
/**
433+
/**
428434
* Infer a tuple of types from a tuple of `Struct`s.
429435
*
430436
* This is used to recursively retrieve the type from `union` `intersection` and
431437
* `tuple` structs.
432438
*/
433439

434440
export type InferStructTupleUncoerced<
435-
Tuple extends AnyStruct[],
436-
Length extends number = Tuple['length']
441+
Tuple extends AnyStruct[],
442+
Length extends number = Tuple['length']
437443
> = Length extends Length
438-
? number extends Length
439-
? Tuple
440-
: _InferTupleUncoerced<Tuple, Length, []>
441-
: never
444+
? number extends Length
445+
? Tuple
446+
: _InferTupleUncoerced<Tuple, Length, []>
447+
: never
442448
type _InferTupleUncoerced<
443-
Tuple extends AnyStruct[],
444-
Length extends number,
445-
Accumulated extends unknown[],
446-
Index extends number = Accumulated['length']
449+
Tuple extends AnyStruct[],
450+
Length extends number,
451+
Accumulated extends unknown[],
452+
Index extends number = Accumulated['length']
447453
> = Index extends Length
448-
? Accumulated
449-
: _InferTuple<Tuple, Length, [...Accumulated, InferUncoerced<Tuple[Index]>]>
454+
? Accumulated
455+
: _InferTuple<Tuple, Length, [...Accumulated, InferUncoerced<Tuple[Index]>]>

0 commit comments

Comments
 (0)