Repository: jquense/yup

Issue #1243: Something is wrong with dates and TypeScript support.
Created: 2021-01-30T09:35:15Z
Describe the bug
The following typescript errors appear:
import * as Yup from 'yup';
import {SchemaOf, Asserts} from 'yup';

// TEST 1

const schema1 = Yup.object({
  date: Yup.date().defined()
}).defined();

type Type1 = Asserts<typeof schema1>;

function test1(schema: SchemaOf<Type1>) {

}

test1(schema1); // TS error
// Argument of type 'DefinedObjectSchema<{ date: DefinedDateSchema<Date | undefined, Record<string, any>>; }, Record<string, any>, TypeOfShape<{ date: DefinedDateSchema<Date | undefined, Record<...>>; }>>' is not assignable to parameter of type 'ObjectSchemaOf<AssertsShape<{ date: DefinedDateSchema<Date | undefined, Record<string, any>>; }>>'.
//  The types of 'fields.date' are incompatible between these types.
//    Type 'DefinedDateSchema<Date | undefined, Record<string, any>>' is missing the following properties from type 'ObjectSchema<{ toString: ObjectSchemaOf<() => string>; toDateString: ObjectSchemaOf<() => string>; toTimeString: ObjectSchemaOf<() => string>; toLocaleString: ObjectSchemaOf<...>; ... 38 more ...; toJSON: ObjectSchemaOf<...>; }, Record<...>, TypeOfShape<...>, AssertsShape<...>>': fields, _sortErrors, _nodes, _excludedEdges, and 11 more.ts(2345)

// TEST 2

interface Type2 {
  date: Date;
}

const schema2: SchemaOf<Type2> = Yup.object({ // TS ERROR
  date: Yup.date().defined()
}).defined();
// Type 'DefinedObjectSchema<{ date: DefinedDateSchema<Date | undefined, Record<string, any>>; }, Record<string, any>, TypeOfShape<{ date: DefinedDateSchema<Date | undefined, Record<...>>; }>>' is not assignable to type 'ObjectSchemaOf<Test2>'.
//   The types of 'fields.date' are incompatible between these types.
//     Type 'DefinedDateSchema<Date | undefined, Record<string, any>>' is not assignable to type 'ObjectSchemaOf<Date>'.ts(2322)


function test2(schema: SchemaOf<Type2>) {

}

test2(schema2);
To Reproduce
https://codesandbox.io/s/yup-dates-typescript-l4kon?file=/src/index.ts
Expected behavior
There should be no typescript errors for this example.
Platform (please complete the following information):

Firefox
85.0

Duplicate issue #1302: TypeScript: SchemaOf throws an error when schema contains date()
Created: 2021-03-18T15:25:12Z
Describe the bug
When the schema contains yup.date().required() and the field is required in the model type, TypeScript complains about incompatible types.
To Reproduce
export interface Model {
  name: string
  startTime: Date
  endTime: Date
}

export const ModelSchema: Yup.SchemaOf<Model> = Yup.object({
  name: Yup.string().required(),
  startTime: Yup.date().defined().required().min(new Date()),
  endTime: Yup.date().defined().required().min(Yup.ref('startTime')),
}).defined()
Error
Type 'DefinedObjectSchema<{ name: RequiredStringSchema<string | undefined, Record<string, any>>; startTime: RequiredDateSchema<Date | undefined, Record<string, any>>; endTime: RequiredDateSchema<...>; }, Record<...>, TypeOfShape<...>>' is not assignable to type 'ObjectSchemaOf<Model>'.
  The types of 'fields.startTime' are incompatible between these types.
    Type 'RequiredDateSchema<Date | undefined, Record<string, any>>' is not assignable to type 'ObjectSchemaOf<Date> | ObjectSchemaOf<Lazy<Date, never>>'.
      Type 'RequiredDateSchema<Date | undefined, Record<string, any>>' is missing the following properties from type 'ObjectSchema<{ type: BaseSchema<Maybe<"lazy">, Record<string, any>, "lazy">; __isYupSchema__: BaseSchema<Maybe<boolean>, Record<string, any>, boolean>; ... 10 more ...; isValidSync: ObjectSchemaOf<...> | ObjectSchemaOf<...>; }, Record<...>, TypeOfShape<...>, AssertsShape<...>>': fields, _sortErrors, _nodes, _excludedEdges, and 11 more.

Expected behavior
TypeScript should not complain. The resulting type should match my model.
Platform (please complete the following information):

Browser [e.g. chrome, safari]: N/A
Version [e.g. 22]: N/A

Additional context
It works when I change the model to:
export interface Model {
  name: string
  startTime: Date | undefined
  endTime: Date | undefined
}
but that's not what I want.

Closing comment (2021-03-21T22:19:21Z): Seems the same as #1243