Preserve tuples when mapping with `as` clauses
See original GitHub issueSearch Terms
#tuple #mapped #as #clause #preserve
Suggestion
New mapped typeās as
clause allows one to filter the entries of an object. Basic mapped types preserve the shape of a tuple but not when used with the new as
clause:
type tuple = [0, 1, 2]
type Mapped<T> = {
[K in keyof T]: T[K] | undefined
}
type MappedAs<T> = {
[K in keyof T as K]: T[K] | undefined
}
type test0 = Mapped<tuple> // tuple shape is preserved
type test1 = MappedAs<tuple> // tuple shape is object now
test0 === [0 | undefined, 1 | undefined, 2 | undefined]
test1 === {
[x: number]: 0 | 1 | 2 | undefined;
0: 0 | undefined;
1: 1 | undefined;
2: 2 | undefined;
length: 3 | undefined;
toString: (() => string) | undefined;
toLocaleString: (() => string) | undefined;
...
}
Use Cases
- Filtering lists without having to rely on costly recursive types
Examples
type FilterNumbers<T> = {
[K in keyof T as T[K] extends number ? never : K]: T[K] | undefined
}
type test2 = FilterNumbers<[1, '2', 3, '4']> // ['2', '4']
Checklist
My suggestion meets these guidelines:
- This wouldnāt be a breaking change in existing TypeScript/JavaScript code
- This wouldnāt change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isnāt a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScriptās Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (9 by maintainers)
Top Results From Across the Web
Tuple or list when using 'in' in an 'if' clause? - python
The CPython interpreter replaces the second form with the first. That's because loading the tuple from a constant is one operation,Ā ...
Read more >Using tuple-wrapping to improve `with` expressions in Elixir
The with expression allows us to declare clauses and their expected results. The clauses are executed in order until the end of the...
Read more >Self-organizing Tuple Reconstruction in Column-stores
First, in order to be able to align all maps in one go we need to actually materialize and maintain all possible maps...
Read more >Python Tuples - GeeksforGeeks
Deleting a Tuple ... Tuples are immutable and hence they do not allow deletion of a part of it. The entire tuple gets...
Read more >8. Compound statements ā Python 3.11.1 documentation
A compound statement consists of one or more 'clauses. ... or a tuple containing an item that is the class or a non-virtual...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
I find myself mostly agreeing with all those points, so maybe weāre talking past each other? Iām going to try one more time and then stop:
I am in favor of
as
clauses mapping tuples to tuples and arrays to arrays, as long as numeric-like indices map to numeric-like indices in the clause. If thatās all you were asking for Iād be ššš. I donāt want to see a tuple become an object either.But deleting numeric keys from an array does not logically result in a shorter filtered array; it results in a sparse array of the same length:
The automatic reindexing youāre talking about which turns a sparse array into a shorter array sounds like a great thing to have in the type system, but I just donāt see how mapped types with
as
clauses should result in that.It certainly canāt be done by a straightforward reading of
K in keyof T as F<K>
whereF<K>
is always eitherK
ornever
. ThatK
is a real index from the keys ofT
. If it gets mapped tonever
, then that key is not present in the output type. If it gets mapped toK
then that key is present in the output type. Thatās how it works with objects, and thatās what Iād expect it to do with tuples also. I donāt have much use for sparse tuples, but thatās what Iād expect to see here.It really feels like Anders gave us a screwdriver and we are now trying to use it to drive nails into a wall. But for that I want a hammer, not a screwdriver which auto-detects nails and sprouts a hammer head. Honestly, though, if such a screwdriver-with-optional-hammer-abilities were given to us, Iām sure Iād use it too. And maybe even be happy about it. But Iād sure feel awkward when explaining it to people on Stack Overflow.
Anyway, good luck with this feature request!
I believe this was a conscious design decision, and in this case I think compacting a tuple to a shorter length is pretty surprising to me. I would have expected a sparse array type in this case.