Skip to content

"Expression produces a union type that is too complex to represent" when using interfaces with large number of params for filtering and narrowing down certain keys #57863

Description

🔎 Search Terms

Expression produces a union type that is too complex to represent, 2590, ts2590

🕗 Version & Regression Information

  • This behavior occurs in 5.4.2 and above. On 5.3.3 it works fine

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.2#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgVTABNNU4BvAKDhrgFdjTgB9BIgLjiToFsAjNAG5K1Wj2ABnCZgDmwAPydyccVNnBOEmFGQy4AX2G04wIglRFmq6XMUUTZi1ck2NcLTqR7DomtgAWmEhIwAA2zJBadsoBQSHhkTCa2roGRrSm5qbMscFhERBRSg5ZlrnxBVrJnt7pNNbqzFDAODAIEEjRKi6Nza3tSNWpPsYNck0t2G0dORB0KF1jLH1TA7PzSe4pXmm+iEihyCwAjnRoAJ5dyIchzKcXQzsjtAGFwEisB0cTEnShMF1XhJ3p8bstJH9Nh5hnU4NhMKFQnwcABrO5nKCXYrwxHI7Bo+6Yx61PYSfwIMBgXToi5dMkUqleGlErY1XbGMDNHL+YD4uYwZlY+yclgBXko-mC4ns2iQRFdOWhaXPGiK5hBCQAdzQCogiPVSC1aGVsJ4525pGc-B1xTNFoF4mtUBNe1iDuATsBgXdTpdxjdzAAVhBkBN7kV7AHg6HmuGodsSf7vcw+BBCgDscnU+m-bRmjwIAA3bIB7MR5T5oslrNpqqsmGUfQiGDnMB4ABiCH+aHb81WHQAPPgTCBUEgiBICIxUAAaOAANRHY4nBAAfHAALxwAAUDE4+AAlJv1wxEJP58JKKBILA4Og+9MkCpSAFO92oEOl+8V4QSLO4AAil+46Tm+qBQABGLnKu257OgXbgZwQEAD6AQA2gAujOlAHpwYE9g+AxDnO+Efvgc4Aau65UP6HQSHqwAAHShBAMjbgARAAsi+5JeOxc7we+B6ws0MB0FAT67mA+64fQYBnnApHEYB64buuACE6kMMITaUAA9AAVAZcAAIKInAhYImwd4IWgcCEggkg7sAxaYnJf54CiwDnCOuBgPADA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GkqOaQOP5XMMmPJeHRLpLE2LsT6WYPic5XlFOaMxd4MgJlwHYvMwpSy7zQHVpctAqyoU0FEuJJ8IQGqNJgKC8FKkYJktoE2XSbtDTwH8MkpKNK4B0uaduVZlBuV8EYh0DiSwVnCFFeKpAkqehyHVlJI8akKD6FWRyrQ7geVbllRKtC7EpUFXKPkRI7EMKrN+GKrJ24VX+PIBqmVyS5XbkNcanFpqEjpgtXOIVDqnWUCAA

💻 Code

exportinterfaceUpdate{update_id: number;message?: {message: string};edited_message?: {edited_message: string};channel_post?: {channel_post: string};edited_channel_post?: {edited_channel_post: string};message_reaction?: {message_reaction: string};message_reaction_count?: {message_reaction_count: string};inline_query?: {inline_query: string};chosen_inline_result?: {chosen_inline_result: string};callback_query?: {callback_query: string};shipping_query?: {shipping_query: string};pre_checkout_query?: {pre_checkout_query: string};poll?: {poll: string};poll_answer?: {poll_answer: string};my_chat_member?: {my_chat_member: string};chat_member?: {chat_member: string};chat_join_request?: {chat_join_request: string};chat_boost?: {chat_boost: string};removed_chat_boost?: {removed_chat_boost: string};}typeFilterFunction<UextendsUpdate,VextendsU>=(up: U)=>up is V;exportfunctionmatchFilter<UextendsUpdate,QextendsFilterQuery>(filter: Q|Q[],): FilterFunction<U,Filter<U,Q>>{// ^ errors outconsole.log("Matching",filter);return(up: U): up is Filter<U,Q>=>!!up;}/** All valid filter queries (every update key except update_id) */exporttypeFilterQuery=keyofOmit<Update,"update_id">;/** Narrow down an update object based on a filter query */exporttypeFilter<UextendsUpdate,QextendsFilterQuery>=PerformQuery<U,RunQuery<Q>>;// generate an object structure that can be intersected with updates to narrow them downtypeRunQuery<Qextendsstring>=Combine<L1Fragment<Q>,Q>;// maps each part of the filter query to Record<"key", object>typeL1Fragment<Qextendsstring>=Qextendsunknown ? Record<Q,object>
: never;// define all other fields from query as keys with value `undefined`typeCombine<U,Kextendsstring>=Uextendsunknown
? U&Partial<Record<Exclude<K,keyofU>,undefined>>
: never;// apply a query result by intersecting it with update,// and then using these values to override the actual updatetypePerformQuery<UextendsUpdate,Rextendsobject>=Rextendsunknown
? FilteredEvent<U,Update&R>
: never;// narrow down an update by intersecting it with a different updatetypeFilteredEvent<EextendsUpdate,UextendsUpdate>=&E&Omit<U,"update_id">;typeMiddleware<UextendsUpdate>=(ctx: U)=>unknown|Promise<unknown>;classEventHub<UextendsUpdate>{use(...middleware: Array<Middleware<U>>): EventHub<U>{console.log("Adding",middleware.length,"generic handlers");returnthis;}on<QextendsFilterQuery>(filter: Q|Q[],
...middleware: Array<Middleware<Filter<U,Q>>>// ^ errors out): EventHub<Filter<U,Q>>{console.log("Adding",middleware.length,"handlers for",filter);returnnewEventHub<Filter<U,Q>>();}}

🙁 Actual behavior

TypeScript compiler throws "Expression produces a union type that is too complex to represent" on Filter type instantiation with large interface

🙂 Expected behavior

TypeScript compiler shouldn't throw "Expression produces a union type that is too complex to represent" on Filter type instantiation with large interface

Additional information about the issue

On 5.3.3, such issue doesn't occur (even when we are adding more parameters to Update interface, e.g. additional 20-30 parameters)
But, on 5.4.2 it seems like limit for it is lowered down. When chat_boost and removed_chat_boost are removed, both Filter instantiations no longer throwing errors

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issueRecent RegressionThis is a new regression just found in the last major/minor version of TypeScript.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions