Repository navigation
TS2590: Expression produces a union type that is too complex to represent, with simple file using TuplesΒ #58268
Copy link
Copy link
Open
Labels
Domain: check: Big UnionsThe root cause is ultimately that big unions interact poorly with complex structuresThe root cause is ultimately that big unions interact poorly with complex structuresHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone
Description
π Search Terms
TS2590.
There are several issues referring to TS2590, but none with the specific, simple reproduction code below.
π Version & Regression Information
This changed between versions 5.0.0-dev.20221117 and 5.0.0-dev.20221118
β― Playground Link
π» Code
tsc --lib esnext one.ts
one.ts:3:7 - error TS2590: Expression produces a union type that is too complex to represent.
3 const itemCopy: TupleItem = item;
~~~~~~~~
Found 1 error in one.ts:3
// one.ts
import {item, type TupleItem} from './two';
const itemCopy: TupleItem = item;// two.ts
export declare const item: TupleItem;
export type TupleItem<Tuple = [1], Property = '0'> = {
value: Tuple extends [1] | [2]
? Property extends '0' | 'some' | 'forEach' | 'map' | 'filter' | 'reduce' | 'reduceRight' | 'find' | 'findIndex' | 'fill' | 'copyWithin' | 'entries' | 'keys' | 'values' | 'includes' | 'flatMap' | 'flat' | 'at'
? Property extends keyof Tuple ? Tuple[Property] : undefined
: undefined
: undefined;
};π Actual behavior
The types fail due to: TS2590: Expression produces a union type that is too complex to represent.
π Expected behavior
The types above are quite simple. Even though the union type has ~20 values, that should not be enough to make the types fail.
Additional information about the issue
Small changes in the code above will fail to reproduce the error, such as:
- Merging both files into a single one
- Not using a
declare constline - Not using generics, or not using generic default types
- Using
Tuple extends ...instead of{value: Tuple extends ...} - Not using
--lib esnext
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Domain: check: Big UnionsThe root cause is ultimately that big unions interact poorly with complex structuresThe root cause is ultimately that big unions interact poorly with complex structuresHelp WantedYou can do thisYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some casesThe current behavior isn't wrong, but it's possible to see that it might be better in some cases
Activity
fatcerberus commentedon Apr 21, 2024
Even with everything in one file, this code just doesn't seem to work at all:
TS Playground
ehmicky commentedon Apr 21, 2024
Thanks for looking into this Bruce Pascoe (@fatcerberus).
That's a different error. This one can be fixed by using this instead:
That failure is expected and relates to whether
Propertycan indexTuple.The bug above instead relates to TypeScript creating a big union type, which makes type checking crash, which is different. To be able to reproduce it, it seems like 2 separate files (exactly identical to the above) must be used.
fatcerberus commentedon Apr 21, 2024
The code as written in the OP produces that error, including when split into two files. That's confusing since it's not the error message you're reporting and makes it look like the repro itself is erroneous.
For what it's worth I wasn't able to reproduce the too-large-union-type error using the bug workbench (which, unlike the playground, does support multiple files):
Bug Workbench
ehmicky commentedon Apr 21, 2024
I have updated the original issue to reflect this, and avoid the confusion with that other unrelated error.
Oh I did not know the Bug Workbench supported multiple files.
You could not reproduce the bug because it requires using
--lib esnext.The following is a reproduction of the bug using the Bug Workbench.
That link works. However, interestingly enough, if you click on the URL (which adds
?lib=lib.esnext.d) then refresh the page, the Bug Workbench fails to load.RyanCavanaugh commentedon Apr 23, 2024
Quick notes
The stack looks like this
We're testing if
To do this we compute the variance of
TupleItemusing instantiation with two marker typesThen we go down the structural comparison on
.value, which results in checkingThis eventually hits the limiting case
because
typeSethas all these types:I believe the definitions in
esnextare near the bottom; the large number of unions here means the cross-product is very large compared to when those definitions aren't presentRyanCavanaugh commentedon Apr 23, 2024
Not really sure this is going to be "fixable". What's the desired behavior of
TupleItem? It's probably possible to write this in a way that doesn't do this computation.daviareias commentedon Jan 17, 2025
This is just typescript avoiding creating a huge type, I got this when I was trying to create a template literal that was the representation of a base 64 5 number plus a sign (e.g. "+00 00 00 00 00"), which equates to 2147483648 combinations