Skip to content

OptionalDeep<any> shows an error  #324

@bboure

Description

@bboure

🐞 Bug Report

Describe the bug

Using OptionalDeep on an object that contains any breaks the generated type.

Reproduce the bug

  type MyType = {
    foo: string;
    bar: any;
  };
  type OptionalType = O.Partial<MyType, 'deep'>;
  const t: OptionalType = {
    foo: 'ok',
    bar: {
      // ts error: type 'string' is not assignable to type 'OptionalDeep<any>'.
      bar: 'baz',
    },
  };


// OptionalType  evaluates to:
type OptionalType = {
    foo?: string | undefined;
    bar?: OptionalDeep<any> | undefined;
}

Expected behavior

OptionalDeep<any> should evaluate to any.

type OptionalType = {
    foo?: string | undefined;
    bar?: any;
}

Possible Solution

Change OptionalDeep to:

export declare type OptionalDeep<O> = O extends object ? {
    [K in keyof O]?: OptionalDeep<O[K]>;
} : O;

Screenshots

image

after proposed fix:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions