Definitelytyped: [@types/yup] InferType from ArraySchema is not compatible with Array type

Created on 13 Feb 2020  路  5Comments  路  Source: DefinitelyTyped/DefinitelyTyped

When InferType is invoked against Yup's validation schema with array as top schema, the resulting type is not compatible with plain Array type.

Note: this works fine for nested array schemas, but unfortunately not with the root one.

To Reproduce

const validationSchema = Yup.array()
  .of(
    Yup.string()
  );

type SectionData = Yup.InferType<typeof validationSchema>;

function arrayHandler(data: SectionData): any[] {
  return data; // error here
}

https://codesandbox.io/s/suspicious-breeze-o9tov

Expected behavior
Resulting type to be compatible with plain array.

Platform:

  • Browser: Latest Chrome
  • Yup Version: 0.28.1
  • @types/yup Version: 0.26.30
  • TS Version: 3.7.2

Authors: @dhardtke @vtserman @MoretonBayRC @sseppola @YashdalfTheGray @vincentjames501 @robertbullen @sat0yu @deskoh @mauricedb @kalley

Most helpful comment

Hey, I'm on 0.29.8 and just ran into this issue too. Here is a codesandbox link reproducing my case.

I don't know if this helps but looking at the error message The expected type comes from property '0' which is declared here on type 'InnerInferTypeArray<string>', it seems like it's considering the array indexes as values too?

All 5 comments

Thanks for the report and the codesandbox! 馃敟

The error message is:

Type 'Id<Partial<Pick<string[], number | (() => string) | (() => string) | (() => number) | ((...items: number[]) => number) | { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; } | ... 23 more ... | ((searchElement: number, fromIndex?: number) => boolean)>> & Pick<...>>' is missing the following properties from type 'any[]': [Symbol.iterator], [Symbol.unscopables]ts(2739)

Seems like the only missing properties are: [Symbol.iterator] and [Symbol.unscopables]

Interesting as the following seems to compile and run just fine:

  const nestedValidationSchema = Yup.object({
    data: Yup.array().of(Yup.string())
  });

  type NestedSectionData = Yup.InferType<typeof nestedValidationSchema>;

  function nestedArrayHandler(data: NestedSectionData): any[] {
    return data.data;
  }

  await expect(nestedArrayHandler({ data })).toBe(data);

@mauricedb - thank you for maintaining these types and implementing this fix! 馃檹馃従

It seems as though this issue may have resurfaced. It works up until version 0.29.3, but it breaks when upgrading to 0.29.4.

Using the following code (slightly modified version of the code you provided - makes everything required):

const nestedValidationSchema = yup.object({
    data: yup
        .array()
        .of(yup.string().required())
        .required() // Adding this required is what seems to break things
}).required();

type NestedSectionData = yup.InferType<typeof nestedValidationSchema>;

function nestedArrayHandler(data: NestedSectionData): string[] {
    return data.data;
}

It's complaining that

Type 'InnerInferTypeArray<string>' is not assignable to type 'string[]'.
  The types returned by 'pop()' are incompatible between these types.
    Type 'Id<Partial<Pick<string, KeyOfUndefined<string>>> & Pick<string, never>> | undefined' is not assignable to type 'string | undefined'.
      Type 'Id<Partial<Pick<string, KeyOfUndefined<string>>> & Pick<string, never>>' is not assignable to type 'string'.

@thekushpatel Interesting, it seems to be getting harder to get the type definitions right. I am currently trying to solve #48595 where the bug or resulting in any is fixed but introduces a new type error but only in TypeScript 4.1.

I am hoping that the current port of Yup to TypeScript is going to solve these issues once and for all.

Hey, I'm on 0.29.8 and just ran into this issue too. Here is a codesandbox link reproducing my case.

I don't know if this helps but looking at the error message The expected type comes from property '0' which is declared here on type 'InnerInferTypeArray<string>', it seems like it's considering the array indexes as values too?

Was this page helpful?
0 / 5 - 0 ratings