Fix type inference for arrays

This commit is contained in:
Moreton Bay RC
2018-01-31 10:36:04 +10:00
parent 8ed0a49ffd
commit 7c30dc8eff
2 changed files with 8 additions and 4 deletions
+4 -4
View File
@@ -118,12 +118,12 @@ export interface DateSchema extends Schema<Date> {
}
export interface ArraySchemaConstructor {
<T>(): ArraySchema<T>;
new<T>(): ArraySchema<T>;
(): ArraySchema<{}>;
new(): ArraySchema<{}>;
}
export interface ArraySchema<T> extends Schema<T[]> {
of(type: Schema<T>): ArraySchema<T>;
of<U>(type: Schema<U>): ArraySchema<U>;
min(limit: number | Ref, message?: string): ArraySchema<T>;
max(limit: number | Ref, message?: string): ArraySchema<T>;
ensure(): ArraySchema<T>;
@@ -132,7 +132,7 @@ export interface ArraySchema<T> extends Schema<T[]> {
export interface ObjectSchemaConstructor {
<T>(fields?: T): ObjectSchema<T>;
new <T>(): ObjectSchema<T>;
new (): ObjectSchema<{}>;
}
export interface ObjectSchema<T> extends Schema<T> {
+4
View File
@@ -266,6 +266,7 @@ interface MyInterface {
stringField: string;
numberField: number;
subFields: SubInterface;
arrayField: string[];
}
interface SubInterface {
@@ -278,6 +279,7 @@ const typedSchema = yup.object<MyInterface>().shape({
subFields: yup.object<SubInterface>().shape({
testField: yup.string().required(),
}).required(),
arrayField: yup.array().of(yup.string()).required(),
});
const testObject = {
@@ -286,6 +288,7 @@ const testObject = {
subFields: {
testField: "test2"
},
arrayField: ["hi"],
};
typedSchema.validate(testObject);
@@ -296,6 +299,7 @@ const untypedSchema = yup.object().shape({
subFields: yup.object().shape({
testField: yup.string().required(),
}).required(),
arrayField: yup.array().of(yup.string()).required(),
});
untypedSchema.validate(testObject);