Giter Site home page Giter Site logo

Comments (6)

ehmicky avatar ehmicky commented on May 5, 2024 1

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.

RyanCavanaugh avatar RyanCavanaugh commented on May 5, 2024 1

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.

fatcerberus avatar fatcerberus commented on May 5, 2024

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.

ehmicky avatar ehmicky commented on May 5, 2024

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.

fatcerberus avatar fatcerberus commented on May 5, 2024

That failure is expected and relates to whether Property can index Tuple.

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.

RyanCavanaugh avatar RyanCavanaugh commented on May 5, 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.

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.