Comments (1)
The last improvements to homomorphic mapped typed instantiated with intersections containing arrays/tuples can be found in this PR: #57801
It even comes with a test case showing the (current) expected behavior for arrays intersected with non-array types (TS playground):
type Box<T> = { value: T };
type Boxify<T> = { [K in keyof T]: Box<T[K]> };
type T5 = Boxify<string[] & { x: string }>;
The result of this is an "expanded" object type. The problem, likely, is that when the result of that gets passed to a spread operator... it won't allow it because this is not an array/tuple type. So it returns an error type upon this instantiation.
However, just spreading arrays/tuples within intersections often "just works" (TS playground):
type Test1<T extends unknown[]> = [...T]
type Input1 = string[] & { a: number }
type Result1 = Test1<Input1>
// ^? type Result1 = string[]
type Input2 = (string | number)[] & ['foo', string, 42]
type Result2 = Test1<Input2>
// ^? type Result2 = (string | 42)[]
type Input3 = [string | boolean, string | symbol, ...number[]] & ['foo', string, 43]
type Result3 = Test1<Input3>
// ^? type Result3 = (string | 43)[]
type Input4 = [string | boolean, boolean, ...number[]] & ['foo', string, 44]
type Result4 = Test1<Input4>
// ^? type Result4 = any[]
It's worth noticing that Result4
displays as any[]
- it's likely that this one is also an error type but I haven't verified it internally.
I'd argue that both should behave the same since the spread mapped type in the example is basically just an Identity
type. We can imagine mapped types with other template types but I think it doesn't matter all that much. The output tuple/array structure can be resolved in situation like this and then the element types can be mapped with the template type.
So I think that isArrayOrTupleOrIntersection
added in #57801 should be changed to use some
over every
and then the non-array/tuple members should be simply ignored. Alternatively, a logic like this could be applied within a "spread context" but making the instantiation logic contextual doesn't make much sense to me here.
from typescript.
Related Issues (20)
- FETCH / Give The json() Function The Power To Use Generic Type HOT 1
- TS 5.6 requires composite projects with noEmit to have fully accessible types, unlike 5.5 HOT 6
- useVsCodeWatcher interferes with updating typescript @aliased imports paths on file or folder moves HOT 2
- Switch typeguard for single case HOT 2
- `AutoFill` is missing a few valid tokens for the `autocomplete` attribute HOT 2
- Cannot read properties of undefined (reading 'charCount') HOT 1
- `tsBuildInfoFile` compiler option should be allowed without `incremental` or `composite` in Typescript versions `>5.6`
- super() typed as returning void HOT 2
- Move to File does not generate imports in the same way that autoimports does
- Move To File makes extra spaces when moving jsdoc comments
- (JSDoc, JavaScript) Imported class types by require() are unresolved HOT 1
- Type all exported members according to some interface HOT 2
- JSDoc typed function comments inside functions
- Error when accessing CSS property value using kebab case in `CSSStyleDeclaration` object HOT 1
- Type 'never' incorrectly inferred for mixed primitive type properties assignment with strictNullChecks disabled HOT 2
- Array of object unions not working as expected HOT 2
- Support WebIDL interfaces nominally (not just structurally) HOT 9
- Improve type inference for nullish coalescing (??) after null checks on multiple variables HOT 2
- [NewErrors] 5.7.0-dev.20240915 vs 5.6.2 HOT 6
- [ServerErrors][JavaScript] 5.7.0-dev.20240915 vs 5.6.2 HOT 3
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.