Skip to content

Commit

Permalink
fix: TS 4.8 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Nov 10, 2022
1 parent d5e9c99 commit bc74c34
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -23,7 +23,7 @@ export type LazyBuilder<

export function create<
TSchema extends ISchema<any, TContext>,
TContext = AnyObject,
TContext extends Maybe<AnyObject> = AnyObject,
>(builder: (value: any, options: ResolveOptions<TContext>) => TSchema) {
return new Lazy<InferType<TSchema>, TContext>(builder);
}
Expand Down
6 changes: 5 additions & 1 deletion src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export type RejectorFn = (
array: readonly any[],
) => boolean;

export function create<C = AnyObject, T = any>(type?: ISchema<T, C>) {
export function create<C extends Maybe<AnyObject> = AnyObject, T = any>(
type?: ISchema<T, C>,
) {
return new ArraySchema<T[] | undefined, C>(type as any);
}

Expand Down Expand Up @@ -120,6 +122,8 @@ export default class ArraySchema<
{
value,
tests,
originalValue: options.originalValue ?? _value,
options,
},
panic,
(innerTypeErrors) => next(innerTypeErrors.concat(arrayErrors), value),
Expand Down
2 changes: 1 addition & 1 deletion src/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = AnyObject,
>(): BooleanSchema<T | undefined, TContext>;
export function create() {
return new BooleanSchema();
Expand Down
8 changes: 4 additions & 4 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends Date, TContext = AnyObject>(): DateSchema<
T | undefined,
TContext
>;
export function create<
T extends Date,
TContext extends Maybe<AnyObject> = AnyObject,
>(): DateSchema<T | undefined, TContext>;
export function create() {
return new DateSchema();
}
Expand Down
8 changes: 4 additions & 4 deletions src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import Schema from './schema';
let isNaN = (value: Maybe<number>) => value != +value!;

export function create(): NumberSchema;
export function create<T extends number, TContext = AnyObject>(): NumberSchema<
T | undefined,
TContext
>;
export function create<
T extends number,
TContext extends Maybe<AnyObject> = AnyObject,
>(): NumberSchema<T | undefined, TContext>;
export function create() {
return new NumberSchema();
}
Expand Down
18 changes: 14 additions & 4 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type { AnyObject };

type MakeKeysOptional<T> = T extends AnyObject ? _<MakePartial<T>> : T;

export type Shape<T extends Maybe<AnyObject>, C = AnyObject> = {
export type Shape<T extends Maybe<AnyObject>, C = any> = {
[field in keyof T]-?: ISchema<T[field], C> | Reference;
};

Expand Down Expand Up @@ -79,7 +79,10 @@ function unknown(ctx: ObjectSchema<any, any, any>, value: any) {

const defaultSort = sortByKeyOrder([]);

export function create<C = AnyObject, S extends ObjectShape = {}>(spec?: S) {
export function create<
C extends Maybe<AnyObject> = AnyObject,
S extends ObjectShape = {},
>(spec?: S) {
type TIn = _<TypeFromShape<S, C>>;
type TDefault = _<DefaultFromShape<S>>;

Expand Down Expand Up @@ -287,12 +290,19 @@ export default class ObjectSchema<
return next;
}

concat<IIn, IC, ID, IF extends Flags>(
concat<IIn extends Maybe<AnyObject>, IC, ID, IF extends Flags>(
schema: ObjectSchema<IIn, IC, ID, IF>,
): ObjectSchema<
ConcatObjectTypes<TIn, IIn>,
TContext & IC,
Extract<IF, 'd'> extends never ? _<ConcatObjectTypes<TDefault, ID>> : ID,
Extract<IF, 'd'> extends never
? // this _attempts_ to cover the default from shape case
TDefault extends AnyObject
? ID extends AnyObject
? _<ConcatObjectTypes<TDefault, ID>>
: ID
: ID
: ID,
TFlags | IF
>;
concat(schema: this): this;
Expand Down
7 changes: 3 additions & 4 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
Message,
InternalOptions,
ExtraParams,
AnyObject,
ISchema,
NestedTestConfig,
} from './types';
Expand Down Expand Up @@ -55,7 +54,7 @@ export type SchemaOptions<TType, TDefault> = {

export type AnySchema<
TType = any,
C = AnyObject,
C = any,
D = any,
F extends Flags = Flags,
> = Schema<TType, C, D, F>;
Expand Down Expand Up @@ -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<TType, TContext, TFlags, TDefault>
Expand Down Expand Up @@ -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 */
Expand Down
8 changes: 4 additions & 4 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ export type MatchOptions = {
let objStringTag = {}.toString();

function create(): StringSchema;
function create<T extends string, TContext = AnyObject>(): StringSchema<
T | undefined,
TContext
>;
function create<
T extends string,
TContext extends Maybe<AnyObject> = AnyObject,
>(): StringSchema<T | undefined, TContext>;
function create() {
return new StringSchema();
}
Expand Down
11 changes: 9 additions & 2 deletions src/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { Flags } from './util/types';

export type { AnyObject, AnySchema };

export interface ISchema<T, C = AnyObject, F extends Flags = any, D = any> {
export interface ISchema<T, C = any, F extends Flags = any, D = any> {
__flags: F;
__context: C;
__outputType: T;
Expand Down Expand Up @@ -65,7 +65,7 @@ export interface ValidateOptions<TContext = {}> {
context?: TContext;
}

export interface InternalOptions<TContext = {}>
export interface InternalOptions<TContext = any>
extends ValidateOptions<TContext> {
__validating?: boolean;
originalValue?: any;
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit bc74c34

Please sign in to comment.