Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ziad-saab committed May 10, 2023
1 parent 702d1e9 commit c269d43
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 24 deletions.
9 changes: 6 additions & 3 deletions src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ export function mask<T, S, C>(
* Check if a value passes a struct.
*/

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

export type InferUncoerced<T extends Struct<any, any, any>> = T['UNCOERCED_TYPE']

export type InferUncoerced<T extends Struct<any, any, any>> =
T['UNCOERCED_TYPE']

/**
* A type utility to describe that a struct represents a TypeScript type.
Expand Down
26 changes: 21 additions & 5 deletions src/structs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export function any(): Struct<any, null> {
* and it is preferred to using `array(any())`.
*/

export function array<T extends Struct<any>>(Element: T): Struct<Infer<T>[], T, InferUncoerced<T>[]>
export function array<T extends Struct<any>>(
Element: T
): Struct<Infer<T>[], T, InferUncoerced<T>[]>
export function array(): Struct<unknown[], undefined>
export function array<T extends Struct<any>>(Element?: T): any {
return new Struct({
Expand Down Expand Up @@ -172,7 +174,11 @@ export function integer(): Struct<number, null> {

export function intersection<A extends AnyStruct, B extends AnyStruct[]>(
Structs: [A, ...B]
): Struct<Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>, null, InferUncoerced<A> & UnionToIntersection<InferStructTupleUncoerced<B>[number]>> {
): Struct<
Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>,
null,
InferUncoerced<A> & UnionToIntersection<InferStructTupleUncoerced<B>[number]>
> {
return new Struct({
type: 'intersection',
schema: null,
Expand Down Expand Up @@ -264,7 +270,9 @@ export function never(): Struct<never, null> {
* Augment an existing struct to allow `null` values.
*/

export function nullable<T, S, C>(struct: Struct<T, S, C>): Struct<T | null, S> {
export function nullable<T, S, C>(
struct: Struct<T, S, C>
): Struct<T | null, S> {
return new Struct({
...struct,
validator: (value, ctx) => value === null || struct.validator(value, ctx),
Expand Down Expand Up @@ -434,7 +442,11 @@ export function string(): Struct<string, null> {

export function tuple<A extends AnyStruct, B extends AnyStruct[]>(
Structs: [A, ...B]
): Struct<[Infer<A>, ...InferStructTuple<B>], null, [InferUncoerced<A>, ...InferStructTupleUncoerced<B>]> {
): Struct<
[Infer<A>, ...InferStructTuple<B>],
null,
[InferUncoerced<A>, ...InferStructTupleUncoerced<B>]
> {
const Never = never()

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

export function union<A extends AnyStruct, B extends AnyStruct[]>(
Structs: [A, ...B]
): Struct<Infer<A> | InferStructTuple<B>[number], null, InferUncoerced<A> | InferStructTupleUncoerced<B>[number]> {
): Struct<
Infer<A> | InferStructTuple<B>[number],
null,
InferUncoerced<A> | InferStructTupleUncoerced<B>[number]
> {
const description = Structs.map((s) => s.type).join(' | ')
return new Struct({
type: 'union',
Expand Down
8 changes: 7 additions & 1 deletion src/structs/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Struct, Context, Validator } from '../struct'
import { object, optional, type } from './types'
import { ObjectSchema, Assign, ObjectType, PartialObjectSchema, ObjectTypeUncoerced } from '../utils'
import {
ObjectSchema,
Assign,
ObjectType,
PartialObjectSchema,
ObjectTypeUncoerced,
} from '../utils'

/**
* Create a new struct that combines the properties properties from multiple
Expand Down
36 changes: 21 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Struct, Infer, Result, Context, Describe, InferUncoerced } from './struct'
import {
Struct,
Infer,
Result,
Context,
Describe,
InferUncoerced,
} from './struct'
import { Failure } from './error'

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


/**
* Omit properties from a type that extend from a specific type.
*/
Expand Down Expand Up @@ -424,26 +430,26 @@ type _InferTuple<
? Accumulated
: _InferTuple<Tuple, Length, [...Accumulated, Infer<Tuple[Index]>]>

/**
/**
* Infer a tuple of types from a tuple of `Struct`s.
*
* This is used to recursively retrieve the type from `union` `intersection` and
* `tuple` structs.
*/

export type InferStructTupleUncoerced<
Tuple extends AnyStruct[],
Length extends number = Tuple['length']
Tuple extends AnyStruct[],
Length extends number = Tuple['length']
> = Length extends Length
? number extends Length
? Tuple
: _InferTupleUncoerced<Tuple, Length, []>
: never
? number extends Length
? Tuple
: _InferTupleUncoerced<Tuple, Length, []>
: never
type _InferTupleUncoerced<
Tuple extends AnyStruct[],
Length extends number,
Accumulated extends unknown[],
Index extends number = Accumulated['length']
Tuple extends AnyStruct[],
Length extends number,
Accumulated extends unknown[],
Index extends number = Accumulated['length']
> = Index extends Length
? Accumulated
: _InferTuple<Tuple, Length, [...Accumulated, InferUncoerced<Tuple[Index]>]>
? Accumulated
: _InferTuple<Tuple, Length, [...Accumulated, InferUncoerced<Tuple[Index]>]>

0 comments on commit c269d43

Please sign in to comment.