From bc74c340721da2ea6e65cb27b967c2970af44d35 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Thu, 10 Nov 2022 09:26:00 -0500 Subject: [PATCH] fix: TS 4.8 compat --- package.json | 2 +- src/Lazy.ts | 4 ++-- src/array.ts | 6 +++++- src/boolean.ts | 2 +- src/date.ts | 8 ++++---- src/number.ts | 8 ++++---- src/object.ts | 18 ++++++++++++++---- src/schema.ts | 7 +++---- src/string.ts | 8 ++++---- src/tuple.ts | 11 +++++++++-- src/types.ts | 4 ++-- yarn.lock | 7 ++++++- 12 files changed, 55 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index d75951bed..60e32d6e5 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "rollup-plugin-filesize": "^9.1.2", "rollup-plugin-node-resolve": "^5.2.0", "synchronous-promise": "^2.0.15", - "typescript": "^4.7.4" + "typescript": "^4.8.4" }, "dependencies": { "property-expr": "^2.0.5", diff --git a/src/Lazy.ts b/src/Lazy.ts index c6d505c15..41af84c60 100644 --- a/src/Lazy.ts +++ b/src/Lazy.ts @@ -13,7 +13,7 @@ import type { SchemaFieldDescription, SchemaLazyDescription, } from './schema'; -import { Flags } from './util/types'; +import { Flags, Maybe } from './util/types'; import { InferType, Schema } from '.'; export type LazyBuilder< @@ -23,7 +23,7 @@ export type LazyBuilder< export function create< TSchema extends ISchema, - TContext = AnyObject, + TContext extends Maybe = AnyObject, >(builder: (value: any, options: ResolveOptions) => TSchema) { return new Lazy, TContext>(builder); } diff --git a/src/array.ts b/src/array.ts index e1eb99d67..e28a7ad4e 100644 --- a/src/array.ts +++ b/src/array.ts @@ -32,7 +32,9 @@ export type RejectorFn = ( array: readonly any[], ) => boolean; -export function create(type?: ISchema) { +export function create = AnyObject, T = any>( + type?: ISchema, +) { return new ArraySchema(type as any); } @@ -120,6 +122,8 @@ export default class ArraySchema< { value, tests, + originalValue: options.originalValue ?? _value, + options, }, panic, (innerTypeErrors) => next(innerTypeErrors.concat(arrayErrors), value), diff --git a/src/boolean.ts b/src/boolean.ts index 2b3160270..d68bb3c8b 100644 --- a/src/boolean.ts +++ b/src/boolean.ts @@ -17,7 +17,7 @@ import isAbsent from './util/isAbsent'; export function create(): BooleanSchema; export function create< T extends boolean, - TContext = AnyObject, + TContext extends Maybe = AnyObject, >(): BooleanSchema; export function create() { return new BooleanSchema(); diff --git a/src/date.ts b/src/date.ts index 73ae34cda..1155869f9 100644 --- a/src/date.ts +++ b/src/date.ts @@ -21,10 +21,10 @@ let isDate = (obj: any): obj is Date => Object.prototype.toString.call(obj) === '[object Date]'; export function create(): DateSchema; -export function create(): DateSchema< - T | undefined, - TContext ->; +export function create< + T extends Date, + TContext extends Maybe = AnyObject, +>(): DateSchema; export function create() { return new DateSchema(); } diff --git a/src/number.ts b/src/number.ts index 8c5d3fc97..3662b0c2e 100644 --- a/src/number.ts +++ b/src/number.ts @@ -18,10 +18,10 @@ import Schema from './schema'; let isNaN = (value: Maybe) => value != +value!; export function create(): NumberSchema; -export function create(): NumberSchema< - T | undefined, - TContext ->; +export function create< + T extends number, + TContext extends Maybe = AnyObject, +>(): NumberSchema; export function create() { return new NumberSchema(); } diff --git a/src/object.ts b/src/object.ts index dc5ce2ac0..39e7b56a6 100644 --- a/src/object.ts +++ b/src/object.ts @@ -28,7 +28,7 @@ export type { AnyObject }; type MakeKeysOptional = T extends AnyObject ? _> : T; -export type Shape, C = AnyObject> = { +export type Shape, C = any> = { [field in keyof T]-?: ISchema | Reference; }; @@ -79,7 +79,10 @@ function unknown(ctx: ObjectSchema, value: any) { const defaultSort = sortByKeyOrder([]); -export function create(spec?: S) { +export function create< + C extends Maybe = AnyObject, + S extends ObjectShape = {}, +>(spec?: S) { type TIn = _>; type TDefault = _>; @@ -287,12 +290,19 @@ export default class ObjectSchema< return next; } - concat( + concat, IC, ID, IF extends Flags>( schema: ObjectSchema, ): ObjectSchema< ConcatObjectTypes, TContext & IC, - Extract extends never ? _> : ID, + Extract extends never + ? // this _attempts_ to cover the default from shape case + TDefault extends AnyObject + ? ID extends AnyObject + ? _> + : ID + : ID + : ID, TFlags | IF >; concat(schema: this): this; diff --git a/src/schema.ts b/src/schema.ts index 8d0f65d99..91b35228d 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -21,7 +21,6 @@ import { Message, InternalOptions, ExtraParams, - AnyObject, ISchema, NestedTestConfig, } from './types'; @@ -55,7 +54,7 @@ export type SchemaOptions = { export type AnySchema< TType = any, - C = AnyObject, + C = any, D = any, F extends Flags = Flags, > = Schema; @@ -134,7 +133,7 @@ export interface SchemaDescription { export default abstract class Schema< TType = any, - TContext = AnyObject, + TContext = any, TDefault = any, TFlags extends Flags = '', > implements ISchema @@ -935,7 +934,7 @@ export default abstract class Schema< export default interface Schema< /* eslint-disable @typescript-eslint/no-unused-vars */ TType = any, - TContext = AnyObject, + TContext = any, TDefault = any, TFlags extends Flags = '', /* eslint-enable @typescript-eslint/no-unused-vars */ diff --git a/src/string.ts b/src/string.ts index beceeb379..8f2e96300 100644 --- a/src/string.ts +++ b/src/string.ts @@ -41,10 +41,10 @@ export type MatchOptions = { let objStringTag = {}.toString(); function create(): StringSchema; -function create(): StringSchema< - T | undefined, - TContext ->; +function create< + T extends string, + TContext extends Maybe = AnyObject, +>(): StringSchema; function create() { return new StringSchema(); } diff --git a/src/tuple.ts b/src/tuple.ts index f3a4d6984..7cc796c4b 100644 --- a/src/tuple.ts +++ b/src/tuple.ts @@ -136,8 +136,15 @@ export default class TupleSchema< }); } - this.runTests({ value, tests }, panic, (innerTypeErrors) => - next(innerTypeErrors.concat(tupleErrors), value), + this.runTests( + { + value, + tests, + originalValue: options.originalValue ?? _value, + options, + }, + panic, + (innerTypeErrors) => next(innerTypeErrors.concat(tupleErrors), value), ); }); } diff --git a/src/types.ts b/src/types.ts index 4000bbca5..a307257e1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -12,7 +12,7 @@ import type { Flags } from './util/types'; export type { AnyObject, AnySchema }; -export interface ISchema { +export interface ISchema { __flags: F; __context: C; __outputType: T; @@ -65,7 +65,7 @@ export interface ValidateOptions { context?: TContext; } -export interface InternalOptions +export interface InternalOptions extends ValidateOptions { __validating?: boolean; originalValue?: any; diff --git a/yarn.lock b/yarn.lock index 044d70204..083c31ee2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9930,11 +9930,16 @@ typescript-workspace-plugin@^2.0.1: resolved "https://registry.yarnpkg.com/typescript-workspace-plugin/-/typescript-workspace-plugin-2.0.1.tgz#3d88be1c35a7fdf2c0160c8cf569ca8993439a12" integrity sha512-xjIYNFlPIA7IWXvnOFJoAeHPbPJSo0AiQDCRJzaAp3+xZwz6maTgeRLB0oEHVtCqz4Q1CDN6U9kh/2z8sxdDBQ== -typescript@>=3.0.1, typescript@^4.5.3, typescript@^4.7.4: +typescript@>=3.0.1, typescript@^4.5.3: version "4.7.4" resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.4.tgz#1a88596d1cf47d59507a1bcdfb5b9dfe4d488235" integrity sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ== +typescript@^4.8.4: + version "4.8.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6" + integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ== + uglify-js@^3.1.4: version "3.4.9" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.4.9.tgz#af02f180c1207d76432e473ed24a28f4a782bae3"