Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
build: fix linting and compatibility with older tsc (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
danroc authored Sep 13, 2023
1 parent 8b51af7 commit 19025af
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/superstruct.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import {
type Infer,
type Context,
Struct,
object as stObject,
} from 'superstruct';
import type { Infer, Context } from 'superstruct';
import { Struct, object as stObject } from 'superstruct';
import type {
ObjectSchema,
OmitBy,
Expand All @@ -20,16 +15,16 @@ export type ExactOptionalTag = {
};

/**
* Exclude a type from the properties of a type.
* Exclude type `Type` from the properties of `Obj`.
*
* ```ts
* type Foo = { a: string | null; b: number };
* type Bar = ExcludeType<Foo, null>;
* // Bar = { a: string, b: number }
* ```
*/
export type ExcludeType<T, V> = {
[K in keyof T]: Exclude<T[K], V>;
export type ExcludeType<Obj, Type> = {
[K in keyof Obj]: Exclude<Obj[K], Type>;
};

/**
Expand All @@ -41,14 +36,17 @@ export type ExcludeType<T, V> = {
* // Bar = { a?: string; b: number}
* ```
*/
export type ExactOptionalize<S extends object> = OmitBy<S, ExactOptionalTag> &
Partial<ExcludeType<PickBy<S, ExactOptionalTag>, ExactOptionalTag>>;
export type ExactOptionalize<Schema extends object> = OmitBy<
Schema,
ExactOptionalTag
> &
Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;

/**
* Infer a type from an superstruct object schema.
*/
export type ObjectType<S extends ObjectSchema> = Simplify<
ExactOptionalize<Optionalize<{ [K in keyof S]: Infer<S[K]> }>>
export type ObjectType<Schema extends ObjectSchema> = Simplify<
ExactOptionalize<Optionalize<{ [K in keyof Schema]: Infer<Schema[K]> }>>
>;

/**
Expand All @@ -58,9 +56,9 @@ export type ObjectType<S extends ObjectSchema> = Simplify<
* @param schema - The object schema.
* @returns A struct representing an object with a known set of properties.
*/
export function object<S extends ObjectSchema>(
schema: S,
): Struct<ObjectType<S>, S> {
export function object<Schema extends ObjectSchema>(
schema: Schema,
): Struct<ObjectType<Schema>, Schema> {
return stObject(schema) as any;
}

Expand Down Expand Up @@ -90,16 +88,16 @@ function hasOptional(ctx: Context): boolean {
* @param struct - The struct to augment.
* @returns The augmented struct.
*/
export function exactOptional<T, S>(
struct: Struct<T, S>,
): Struct<T | ExactOptionalTag, S> {
export function exactOptional<Type, Schema>(
struct: Struct<Type, Schema>,
): Struct<Type | ExactOptionalTag, Schema> {
return new Struct({
...struct,

validator: (value, ctx) =>
!hasOptional(ctx) || struct.validator(value, ctx),

refiner: (value, ctx) =>
!hasOptional(ctx) || struct.refiner(value as T, ctx),
!hasOptional(ctx) || struct.refiner(value as Type, ctx),
});
}

0 comments on commit 19025af

Please sign in to comment.