Mapped tuples types iterates over all properties
See original GitHub issueTypeScript Version: 3.2.0-dev.20181019
Search Terms: mapped tuples reify
Code
type Foo = ['a', 'b'];
interface Bar
{
a: string;
b: number;
}
type Baz = { [K in keyof Foo]: Bar[Foo[K]]; }; // Expected Baz to be [string, number]
Expected behavior: Baz should be [string, number]
Actual behavior: Type ‘[“a”, “b”][K]’ cannot be used to index type ‘Bar’.
Related Issues: https://github.com/Microsoft/TypeScript/issues/25947
Given the Mapped tuple types feature (#25947). I’d expect the code above to work cleanly.
However, I still need to do:
type WorkingBaz = { [K in Exclude<keyof Foo, keyof any[]>]: Foo[K] extends keyof Bar ? Bar[Foo[K]] : never; } & { length: Foo['length']; } & any[];
To have an equivalent type. As far as I understand, the “K” in a mapped type on a tuple should iterate only on numeric keys of this tuple. Therefore, Foo[K] should always be a valid key for Bar…
Issue Analytics
- State:
- Created 5 years ago
- Reactions:43
- Comments:31 (7 by maintainers)
Top Results From Across the Web
Why are Typescript Mapped Tuple Types behaving differently ...
The issue here is that we only map to tuple and array types when we instantiate a generic homomorphic mapped type for a...
Read more >TypeScript 3.1 Adds Mappable Tuple and Array Types - InfoQ
Nearly every JavaScript application needs to map over values in a list, which is a pattern that gets simplified with ES2015 via rest...
Read more >Documentation - Mapped Types - TypeScript
Mapped types build on the syntax for index signatures, which are used to declare the types of properties which have not been declared...
Read more >A Generic Mapper with Value Tuples and Generic Tests.
Mapping is the process of transferring data from a producer class to ... of type PropertyInfo that holds the metadata for every property...
Read more >Using the Iterate Operator - TIBCO Software
Introduction · By default, Iterate emits one tuple for each element in the list. · By default, the emitted tuples contain all of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is because, despite mapped types not behaving as such,
keyof ["some", "tuple"]
still returns0 | 1 | "length" | ... | "whatever"
instead of0 | 1
.@lemoinem You can get your current example to narrow correctly and avoid the behavior @weswigham mentioned with a couple of helper conditional types. Note: this comes with some not so great drawbacks… You lose all array prototype methods and properties, which for my current use case–messing with React hooks–that tradeoff isn’t the worst thing in the world.
The way this type ends up formatting isn’t the greatest, but it ultimately maps to some of your desired outcomes. I am having issues around some constructs I thought would work, similar to @phiresky 's examples. I will get some better examples together and document those, but for now just wanted to provide a bit of a workaround.