GithubHelp home page GithubHelp logo

Comments (6)

ahejlsberg avatar ahejlsberg commented on April 28, 2024 2

I'm considering the best way to fix this. The core issue is that mapped types applied to array intersections do not produce arrays, and that's is a design limitation. However, the error in the repro is a regression, which is unfortunate.

The workaround is to remove the constraint from the type parameter and instead add it through an indirection:

type MustBeArray<T extends any[]> = T

type Hmm2<T> = T extends number[] ?
    MustBeArray<{ [I in keyof T]: 1 }> :
    never;

type Hmm<T extends any[]> = Hmm2<T>;

type X = Hmm<[3, 4, 5]>

from typescript.

ahejlsberg avatar ahejlsberg commented on April 28, 2024 1

Right, the core issue is that when a homomorphic mapped type is applied to an intersection of array types, the resulting type isn't an array type:

type CheckArray<T extends any[]> = T;

type Mappy<T> = { [K in keyof T]: 1 };

type T0 = CheckArray<Mappy<[10, 20, 30]>>;  // [1, 1, 1]
type T1 = CheckArray<Mappy<string[]>>;  // 1[]
type T2 = CheckArray<Mappy<string[] & number[]>>;  // Error

The reason the error shows up in the OP is that with #57122 (which is in 5.4) we now recognize that the conditional type adds the constraint number[] to T in the true branch, meaning that when T is any[] we get the constraint any[] & number[].

The fix here is probably to distribute the homomorphic mapped type over intersections, i.e. treat Mappy<string[] & number[]> as Mappy<string[]> & Mappy<number[]>, but I need to think on this a bit.

from typescript.

Andarist avatar Andarist commented on April 28, 2024

The constraint of T becomes number[] & any[] here (it comes from getBaseConstraint(getSubstitutionIntersection(t))) and that's not exactly something that satisfies isArrayOrTupleType check in getResolvedApparentTypeOfMappedType.

from typescript.

jcalz avatar jcalz commented on April 28, 2024

Yeah, homomorphic mapped types "should" distribute over intersections, but I guess I find it a little surprising that the combination of two array constraints just produces an intersection to begin with, since intersections of array types are so problematic.

from typescript.

ehoogeveen-medweb avatar ehoogeveen-medweb commented on April 28, 2024

I would (perhaps naively) expect that number[] & string[] reduces to never or never[] and number[] & any[] reduces to number[] (or if any[] is weird, then at least with unknown[]). Is that (deliberately) not the case?

from typescript.

jcalz avatar jcalz commented on April 28, 2024

Could someone triage this? Is it a bug/limitation/intended? If it's going to stick around like this for a while is there any workaround for people who run into it? Thanks!

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.