-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assume arity of tuples when declared as literal #24350
Comments
We did consider this when we were first adding support for tuples, and thought it was too big of a breaking change to add.. there are a few issues already open proposing new syntax to address this: #16656 and #10195. having said that, i would like to bring this back to discussion. i gave it a try today, and there were not that many breaks in our Real World Code, which is frankly surprising to me.. they fall into two categories: var x = [1, 2];
x = array; // Error array is not assignable to [number, number] type A = { foo: string, baz: string[]} ;
var a = <A> { baz: ["baz"] }; // type assertion now fails var c = [[1, 2]].concat([[3]]); // [[number]] is not assignable to [number,number] | ConcatArray<[number, number]> |
I think this would be a good change. Tuples now fell a bit unhandy and with this change people could actually use them more. And with #24897 tuples become even more useful. But i think automatic code fixes for some errors described by @mhegazy would be necessary (plus of course a flag to get old behavior - as it's done for other breaking changes) |
@mhegazy In the middle example above, it should work if you instead do I frequently use tuples inside e.g. array declare const names: string[];
names
.map((name, i) => [i, name]) // type widens to (number | string)[]
.filter(...)
.forEach(([i, name]) => {
// i: string | number
// name: string | number
}); |
Yes; and |
This would be a godsend for people using typescript in jsdoc mode. Object.entries(obj)
.map([k, v] => [k, k.toLowerCase()] as [string, string] , but a few chained hits of the jsdoc equivalent Object.entries(obj)
.map([k, v] => /** @type {[string, string]} */ ([k, k.toLowerCase()]) just makes me want to turn off my 'lint with tsc' step, not to mention irritates the gonads off anyone on my project who is not a typescript native. |
I would really like this to be a thing, but until it is, with the new rich tuple support there's a simple workaround. function tuple<T extends any[]>(...items: T): T {
return items;
} // old version
Enums.values(TileTemplateType)
.map<[TileTemplateType, TranslationGenerator]>(type => [type, Translation.generator(TileTemplateType[type])])
.collect(Collectors.toArray)
.sort(([, t1], [, t2]) => Text.toString(t1).localeCompare(Text.toString(t2)))
.values()
.map<IDropdownOption<TileTemplateType>>(([id, t]) => [id, (option: Button) => option.setText(t)])
// new version
Enums.values(TileTemplateType)
.map(type => tuple(type, Translation.generator(TileTemplateType[type])))
.collect(Collectors.toArray)
.sort(([, t1], [, t2]) => Text.toString(t1).localeCompare(Text.toString(t2)))
.values()
.map(([id, t]) => tuple(id, (option: Button) => option.setText(t))) Hope this helps somebody! P.S. .......add...a.....flag? 😉 |
I WILL TURN THIS CAR AROUND |
It seems like #29510 might help for this case? I haven't tested it out yet but here's to hoping. |
@aboyton Even if it does solve the same issue, it's still a workaround, and it requires 2-4 more characters than the |
I've bumped into this problem more times than I would like:
Yes, it can be fixed with a cast, but also, why isn't it enough to specify the enclosing function's return type? |
When storing a tuple in a variable like that, the type is set to This could be a thing, by adding in logic that allowed variables which are created and then immediately returned to not get that default At least, I think this is how it works. Someone correct me if I'm wrong. |
Generally I try to avoid tuples (because it's not clear later what meaning has values inside), but there is one native usecase, where unfortunately there is no other option: constructing Maps.
|
Bump: IMO doing flow analysis on variables whose declared with |
Search Terms
tuples, length, arity
Suggestion
Now that #17765 is out I'm curious about if we could change the arity of tuples declared as literals. This was proposed as part of #16896 but I thought it might be better to pull this part out to have a discussion about this part of that proposal.
With fixed length tuples TypeScript allows you to convert
[number, number]
tonumber[]
but not the other way around (which is great).If you declare a constant such as
foo
above it would be nice if it would be nice if it would have the length as part of its type, that is, iffoo
was assumed to be of type[number, number]
notnumber[]
.This would have potential issues with mutable arrays allowing you to call
push
andpop
although this isn't different to present and was discussed a bit in #6229.Use Cases
When using a function such as
fromPairs
from lodash it requires that the type is a list of tuples. A simplified version isIf I do
it works because the type passed into
fromPairs
is[string, number][]
, but if I try to say map the values to double their value I get a compile error:as the parameter is of type
(string | number)[][]
This can be fixed by going
but this is cumbersome.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: