Comments (6)
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.
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.
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.
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.
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.
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)
- [NewErrors] 5.5.0-dev.20240421 vs 5.4.5 HOT 63
- [ServerErrors][TypeScript] 5.5.0-dev.20240421 HOT 14
- [ServerErrors][JavaScript] 5.5.0-dev.20240421 HOT 8
- `T extends infer I1 extends { K: infer I2 }` infers I2 to unknown HOT 6
- Wrong type narrowing on union type. HOT 2
- Document link for reference types and path
- object with `?:` property type, spread last --> allowed (but shouldn't) HOT 3
- Add a flag to require JS object literals to be initialized with all declared members HOT 4
- Type inference lost after spreading array with `ArrayLike` HOT 1
- Regular Expression finds HOT 4
- "This comparison appears to be unintentional" and control flow analysis with a variable updated in lambda HOT 4
- Classes static generic function return `any` type. HOT 2
- Is assignment not allowed here? HOT 2
- Editor changes overload resolution based on syntax HOT 5
- TS cache corruption leads to "error TS2590: Expression produces a union type that is too complex to represent" HOT 1
- Type alias circularly references itself (5.4 regression) HOT 1
- error TS2385: Overload signatures must all be public, private or protected. HOT 2
- `export type * ...` statements in `.d.ts` files do not work (5.4 regression) HOT 4
- HTMLFormElement disallows symbol keys HOT 5
- TypeScript language service cannot find subclass references/implementation of mixin methods
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.