Skip to content

Expression produces a union type that is too complex to represent #44075

Description

@MichaelTontchev

Bug Report

🔎 Search Terms

union type that is too complex to represent

🕗 Version & Regression Information

TS version: 4.2.3

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about union types too complex to represent

⏯ Playground Link

Playground link with relevant code

💻 Code

I have a function that should be able to take an object along with a key name that will be present within that object with a specific type, which in this example I hardcode to string to simplify the problem:

type WithStringProperty<K extends string> = {
	[P in K]: string;
};

function extractProperty<K extends string>(wrapper: WithStringProperty<K>, key: K) {
	return wrapper[key];
}

This much works. But when I make my wrapper parameter a little more complex by wrapping it in DeepReadonly, it fails with the error from the title:

function extractProperty<K extends string>(wrapper: DeepReadonly<WithStringProperty<K>>, key: K) {
	return wrapper[key];
}

DeepReadonly is from ts-essentials:

export declare type DeepReadonly<T> = T extends Builtin
  ? T
  : T extends {}
  ? {
      readonly [K in keyof T]: DeepReadonly<T[K]>;
    }
  : Readonly<T>;

export declare type Builtin = Primitive | Function | Date | Error | RegExp;
export declare type Primitive = string | number | boolean | bigint | symbol | undefined | null;

It doesn't seem like the combination should introduce some large combination of possible union types, but I guess I'm wrong?

🙁 Actual behavior

Error: Expression produces a union type that is too complex to represent

🙂 Expected behavior

No error

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions