Skip to content

Mapped type over any incorrectly rejects structural assignability #4451

Description

@tmadeira

Behavior difference: tsgo produces different results than TypeScript 6.0.

Steps to reproduce

Repository with minimal code to reproduce the issue: https://github.com/tmadeira/ts7-yup-bug

repro.ts (requires yup@1.6.1):

import { object, string, number, ObjectSchema } from "yup";
import type { AnyObjectSchema } from "yup";

interface MyValues {
  foo: string;
  bar: number;
}

const specific: ObjectSchema<MyValues> = object({
  foo: string().required(),
  bar: number().required(),
});

const test: AnyObjectSchema = specific;

tsconfig.json:

{
  "compilerOptions": {
    "strict": true,
    "moduleResolution": "bundler",
    "esModuleInterop": true
  },
  "include": ["repro.ts"]
}

The core issue is in how tsgo evaluates Yup's mapped type Shape:

type Shape<T extends Maybe<AnyObject>, C = any> = {
  [field in keyof T]-?: ISchema<T[field], C> | Reference;
};

When T = any, keyof any = string | number | symbol, so Shape<any, any> should structurally satisfy any concrete Shape<MyValues, AnyObject>. tsgo instead treats Shape<any, any> as having no specific keys.

Behavior with typescript@6.0

No errors. tsc 6.0.3 correctly recognizes that ObjectSchema<MyValues> is structurally assignable to AnyObjectSchema (ObjectSchema<any, any, any, any>).

Behavior with tsgo

tsgo 7.0.0-dev.20260624.1 reports TS2322:

repro.ts(14,7): error TS2322: Type 'ObjectSchema<MyValues, AnyObject, any, "">' is not assignable to type 'AnyObjectSchema'.
  The types returned by 'optional().concat' are incompatible between these types.
    ...
      Types of parameters 'schema' and 'schema' are incompatible.
        Type 'ObjectSchema<any, any, any, any>' is not assignable to type 'ObjectSchema<MyValues | undefined, AnyObject, any, "">'.
          The types returned by 'default(...).fields' are incompatible between these types.
            Type 'Shape<any, any>' is missing the following properties from type 'Shape<MyValues, AnyObject>': foo, bar

The terminal error — Shape<any, any> is missing properties foo, bar — shows that tsgo does not recognize the mapped type { [field in keyof any]-?: ... } as covering all possible keys.

In our project (yup 1.6.1, strict mode), this produces many errors. The ObjectSchema<ConcreteType>AnyObjectSchema pattern is pervasive in Yup-based form validation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working As IntendedThis issue describes intentional behavior.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions