edgeguide / expect

Validates user input at runtime
MIT License
1 stars 0 forks source link

Type inference does not work for union types #6

Open marwankhalili opened 4 years ago

marwankhalili commented 4 years ago

This is a problem e.g. when passing a function to the items option:

expect(
  {
    users: {
      type: "array",
      items: (user: { isLoggedIn: boolean }) => ({
        type: "object",
        keys: user.isLoggedIn
          ? {
              username: "string",
              password: "string",
              isLoggedIn: { type: "boolean", allowNull: true },
            }
          : {
              temporaryUuid: "string",
              isLoggedIn: { type: "boolean", allowNull: true },
            },
      }),
    },
  },
  {
    users: [
      { isLoggedIn: true, username: "John", password: "Snow" },
      { isLoggedIn: false, temporaryUuid: '123' },
    ],
  }
).getParsed(); // { users?: { isLoggedIn: boolean | "" | null | undefined; }[] | undefined } }

/* getParsed() should return the following type
{
  users?: 
  | {
    username: string;
    password: string;
    isLoggedIn: boolean | undefined | null;
  }[] 
  | {
    temporaryUuid: string;
    isLoggedIn: boolean | undefined | null;
  }[]
}
*/
marwankhalili commented 4 years ago

The type inference used by errors() has the same issue