Comments (6)
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.
I have updated the original issue to reflect this, and avoid the confusion with that other unrelated error.
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):
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.
from typescript.
Quick notes
The stack looks like this
We're testing if
TupleItem<[1], "0"> --assignable to--> TupleItem
To do this we compute the variance of TupleItem
using instantiation with two marker types
TupleItem<?_1, Property> --assignable to--> TupleItem<?_2, Property>
Then we go down the structural comparison on .value
, which results in checking
? extends [1] | [2] ? Property extends "0" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight" | "find" | "findIndex" | "fill" | "copyWithin" | ... 6 more ... | "at" ? Property extends keyof ? ? ?[Property] : undefined : undefined : undefined
--assignable to-->
? extends [1] | [2] ? Property extends "0" | "some" | "forEach" | "map" | "filter" | "reduce" | "reduceRight" | "find" | "findIndex" | "fill" | "copyWithin" | ... 6 more ... | "at" ? Property extends keyof ? ? ?[Property] : undefined : undefined : undefined
This eventually hits the limiting case
// We are attempting to construct a type of the form X & (A | B) & (C | D). Transform this into a type of
// the form X & A & C | X & A & D | X & B & C | X & B & D. If the estimated size of the resulting union type
// exceeds 100000 constituents, report an error.
if (!checkCrossProductUnion(typeSet)) {
because typeSet
has all these types:
'1 | 2'
'((predicate: (value: 1, index: number, array: 1[]) => unknown, thisArg?: any) => boolean) | ((predicate: (value: 2, index: number, array: 2[]) => unknown, thisArg?: any) => boolean)'
'((callbackfn: (value: 1, index: number, array: 1[]) => void, thisArg?: any) => void) | ((callbackfn: (value: 2, index: number, array: 2[]) => void, thisArg?: any) => void)'
'(<U>(callbackfn: (value: 1, index: number, array: 1[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: 2, index: number, array: 2[]) => U, thisArg?: any) => U[])'
'{ <S extends 1>(predicate: (value: 1, index: number, array: 1[]) => value is S, thisArg?: any): S[]; (predicate: (value: 1, index: number, array: 1[]) => unknown, thisArg?: any): 1[]; } | { <S extends 2>(predicate: (value: 2, index: number, array: 2[]) => value is S, thisArg?: any): S[]; (predicate: (value: 2, index...'
'{ (callbackfn: (previousValue: 1, currentValue: 1, currentIndex: number, array: 1[]) => 1): 1; (callbackfn: (previousValue: 1, currentValue: 1, currentIndex: number, array: 1[]) => 1, initialValue: 1): 1; <U>(callbackfn: (previousValue: U, currentValue: 1, currentIndex: number, array: 1[]) => U, initialValue: U): U;...'
'{ (callbackfn: (previousValue: 1, currentValue: 1, currentIndex: number, array: 1[]) => 1): 1; (callbackfn: (previousValue: 1, currentValue: 1, currentIndex: number, array: 1[]) => 1, initialValue: 1): 1; <U>(callbackfn: (previousValue: U, currentValue: 1, currentIndex: number, array: 1[]) => U, initialValue: U): U;...'
'{ <S extends 1>(predicate: (value: 1, index: number, obj: 1[]) => value is S, thisArg?: any): S | undefined; (predicate: (value: 1, index: number, obj: 1[]) => unknown, thisArg?: any): 1 | undefined; } | { ...; }'
'((predicate: (value: 1, index: number, obj: 1[]) => unknown, thisArg?: any) => number) | ((predicate: (value: 2, index: number, obj: 2[]) => unknown, thisArg?: any) => number)'
'((value: 1, start?: number | undefined, end?: number | undefined) => [1]) | ((value: 2, start?: number | undefined, end?: number | undefined) => [2])'
'((target: number, start: number, end?: number | undefined) => [1]) | ((target: number, start: number, end?: number | undefined) => [2])'
'(() => IterableIterator<[number, 1]>) | (() => IterableIterator<[number, 2]>)'
'(() => IterableIterator<number>) | (() => IterableIterator<number>)'
'(() => IterableIterator<1>) | (() => IterableIterator<2>)'
'((searchElement: 1, fromIndex?: number | undefined) => boolean) | ((searchElement: 2, fromIndex?: number | undefined) => boolean)'
'(<U, This = undefined>(callback: (this: This, value: 1, index: number, array: 1[]) => U | readonly U[], thisArg?: This | undefined) => U[]) | (<U, This = undefined>(callback: (this: This, value: 2, index: number, array: 2[]) => U | readonly U[], thisArg?: This | undefined) => U[])'
'(<A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]) | (<A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[])'
'((index: number) => 1 | undefined) | ((index: number) => 2 | undefined)'
I believe the definitions in esnext
are near the bottom; the large number of unions here means the cross-product is very large compared to when those definitions aren't present
from typescript.
Even with everything in one file, this code just doesn't seem to work at all:
TS Playground
Type 'Property' cannot be used to index type 'Tuple'.
from typescript.
Thanks for looking into this @fatcerberus.
That's a different error. This one can be fixed by using this instead:
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;
};
That failure is expected and relates to whether Property
can index Tuple
.
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.
from typescript.
That failure is expected and relates to whether
Property
can indexTuple
.
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
from typescript.
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.
from typescript.
Related Issues (20)
- Module resolution: typeRoots key conflict in cache HOT 2
- JSDoc @import not detecting use in @callback HOT 2
- Segmentation fault on Node.js 22 HOT 27
- Constraint in generic type forgotten in assignability check
- tsc crash with "TypeError: Cannot read properties of undefined (reading 'flags')" HOT 5
- TS hangs up entire computer, type of variables stuck at "Loading..."
- Use properties of main object as context parameter of function that belongs to property of main object
- staging-typescript website downtime HOT 4
- Unnecessary any returned from Object.values and Object.entries HOT 10
- Missing reachability error on non-const enum
- Iterators/Generators, next(), and unsafe yield statement types
- Type inference: is that a bug? HOT 3
- [ID] `--isolatedDeclarations` disallows `const` initialized with a regex literal HOT 3
- Icons are completely messed up in Visual Studio member list HOT 4
- Although "Only use Tab or Enter to commit" is off, completions are not committed by obvious next character HOT 1
- `Move` Code Action Constraints HOT 2
- Add preferTypeOnlyAutoImports to protocol.UserPreferences
- typeof with identically named type and value causes invalid .d.ts output HOT 7
- Surprising circularity when parameter within base type expression refers to the class itself
- [isolatedDeclarations][5.5] Type annotations needed for literals containing enum values HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.