Skip to content

Commit

Permalink
fix: don't return any for oneOf
Browse files Browse the repository at this point in the history
ref #1675
  • Loading branch information
jquense committed Aug 23, 2022
1 parent c49fee8 commit 74c5bc5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export default class BooleanSchema<
return super.default(def);
}

// concat<TOther extends BooleanSchema<any, any>>(schema: TOther): TOther;
defined(
msg?: Message,
): BooleanSchema<Defined<TType>, TContext, TDefault, TFlags> {
Expand Down
8 changes: 8 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,14 @@ export default abstract class Schema<
return next;
}

oneOf<U extends TType>(
enums: ReadonlyArray<U | Reference>,
message?: Message<{ values: any }>,
): this;
oneOf(
enums: ReadonlyArray<TType | Reference>,
message: Message<{ values: any }>,
): any;
oneOf<U extends TType>(
enums: ReadonlyArray<U | Reference>,
message = locale.oneOf,
Expand Down
6 changes: 5 additions & 1 deletion src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,13 @@ export default interface StringSchema<
): StringSchema<TType, TContext, D, ToggleDefault<TFlags, D>>;

oneOf<U extends TType>(
arrayOfValues: ReadonlyArray<U | Reference>,
arrayOfValues: ReadonlyArray<U | Reference<U>>,
message?: MixedLocale['oneOf'],
): StringSchema<U | Optionals<TType>, TContext, TDefault, TFlags>;
oneOf(
enums: ReadonlyArray<TType | Reference>,
message?: Message<{ values: any }>,
): this;

concat<UType extends Maybe<string>, UContext, UDefault, UFlags extends Flags>(
schema: StringSchema<UType, UContext, UDefault, UFlags>,
Expand Down
16 changes: 15 additions & 1 deletion test/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ import ObjectSchema, { create as object } from '../../src/object';

import { _ } from '../../src/util/types';

Base_methods: {
// $ExpectType boolean | undefined
bool().oneOf([true, ref('$foo')]).__outputType;

// $ExpectType "asf" | "foo" | undefined
string().oneOf(['asf', ref<'foo'>('$foo')]).__outputType;

// $ExpectType Date | undefined
date().oneOf([new Date(), ref('$now')]).__outputType;

// $ExpectType number | undefined
number().oneOf([1, ref('$foo')]).__outputType;
}

Mixed: {
const mxRequired = mixed<string | number>().required();

Expand Down Expand Up @@ -218,7 +232,7 @@ Strings: {
string().oneOf(['foo', 'bar']).__outputType;

// $ExpectType "foo" | "bar" | null | undefined
string().nullable().oneOf(['foo', 'bar', null]).__outputType;
string().nullable().oneOf(['foo', 'bar']).__outputType;
}

Numbers: {
Expand Down

0 comments on commit 74c5bc5

Please sign in to comment.