Yup - preserve null & undefined in Shape & InferType (#43046)

This commit is contained in:
Liam Goodacre 2020-03-31 23:49:14 +01:00 committed by GitHub
parent 964674cbc1
commit 15e3a6cd6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 7 deletions

14
types/yup/index.d.ts vendored
View File

@ -236,10 +236,9 @@ export type ObjectSchemaDefinition<T extends object | null | undefined> = {
* This is conducive to the functionality of
* [yup's `object.shape()` method](https://www.npmjs.com/package/yup#objectshapefields-object-nosortedges-arraystring-string-schema).
*/
export type Shape<T extends object | null | undefined, U extends object> = {
[P in keyof T]: P extends keyof U ? U[P] : T[P];
} &
U;
export type Shape<T extends object | null | undefined, U extends object> =
| ({ [P in keyof T]: P extends keyof U ? U[P] : T[P]; } & U)
| PreserveOptionals<T>;
export interface ObjectSchemaConstructor {
<T extends object>(fields?: ObjectSchemaDefinition<T>): ObjectSchema<T>;
@ -512,8 +511,13 @@ type KeyOfUndefined<T> = {
[P in keyof T]-?: undefined extends T[P] ? P : never;
}[keyof T];
type PreserveNull<T> = T extends null ? null : never;
type PreserveUndefined<T> = T extends undefined ? undefined : never;
type PreserveOptionals<T> = PreserveNull<T> | PreserveUndefined<T>;
type Id<T> = { [K in keyof T]: T[K] };
type RequiredProps<T> = Pick<T, Exclude<keyof T, KeyOfUndefined<T>>>;
type NotRequiredProps<T> = Partial<Pick<T, KeyOfUndefined<T>>>;
type InnerInferType<T> = T extends Array<infer T> ? T[] : Id<NotRequiredProps<T> & RequiredProps<T>> ;
type InnerInferType<T> =
| (T extends Array<infer T> ? T[] : Id<NotRequiredProps<T> & RequiredProps<T>>)
| PreserveOptionals<T>;
type InferredArrayType<T> = T extends Array<infer U> ? U : T;

View File

@ -680,7 +680,7 @@ const definitionBC: yup.ObjectSchemaDefinition<BC> = {
b: yup.string(),
c: yup.number(),
};
const combinedSchema = yup.object(definitionAB).shape(definitionBC); // $ExpectType ObjectSchema<Shape<AB, BC>>
const combinedSchema = yup.object(definitionAB).shape(definitionBC); // $ExpectType ObjectSchema<{ a: string; b: string; } & BC>
// $ExpectError
yup.object<MyInterface>({
@ -848,5 +848,35 @@ const resultingSchema2 = wrapper<string | number>(true, yup.mixed().oneOf(['1',
const arrayOfStringsSchema = yup.array().of(yup.string());
type ArrayOfStrings = yup.InferType<typeof arrayOfStringsSchema>;
function returnTheArray(data: ArrayOfStrings): any[] {
return data;
return data;
}
const topLevelStringNullable = yup.string().nullable();
const topLevelStringNullableExample: yup.InferType<typeof topLevelStringNullable> = null;
const topLevelObjectNullable = yup.object().nullable();
const topLevelObjectNullableExample: yup.InferType<typeof topLevelObjectNullable> = null;
const topLevelArrayNullable = yup.array().nullable();
const topLevelArrayNullableExample: yup.InferType<typeof topLevelArrayNullable> = null;
const nestedNullableShape = yup.object().shape({
foo: yup.object().nullable().shape({})
});
const nestedNullableShapeExample: yup.InferType<typeof nestedNullableShape> = {
foo: null
};
const nestedShapeNullable = yup.object().shape({
foo: yup.object().shape({}).nullable()
});
const nestedShapeNullableExample: yup.InferType<typeof nestedShapeNullable> = {
foo: null
};
const nestedArrayNullable = yup.object().shape({
foo: yup.array().nullable()
});
const nestedArrayNullableExample: yup.InferType<typeof nestedArrayNullable> = {
foo: null
};