You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't think TypeScript can help in this case because switch working with arrays would be quite different from JavaScript's behaviour. You probably should support the ECMAScript pattern matching proposal. If that's accepted into the standard, you will get tuple matching and more.
If the programming language has a "tuple" concept, it means it can match with other tuple.
There is nothing to do with array, though you use array to simulate tuple.
Martin Böh (@MartB), thanks. I solved it what as you writes.
But as I suggest, typescript has a "tuple" concept, then it should has an ability to match tuple in switch.
Pattern matching isn't really a type-system concept. There are plenty of languages with pattern matching but no compile-time types. (Erlang, Clojure, Common Lisp and Scheme come to mind immediately.)
But as I suggest, typescript has a "tuple" concept, then it should has an ability to match tuple in switch.
JavaScript does not switch on non-primatives (references). You cannot switch on Objects, Arrays, etc..
While a tuple type is a TypeScript concept, it is still a non-primative concept. As tuples stand at the moment, they are a design time only concept, with their typing information fully erasable by using Array literals at run-time. Implementing this feature it would break several of the TypeScript Design Goals:
Impose no runtime overhead on emitted programs.
Preserve runtime behavior of all JavaScript code.
And would be an anti-goal:
Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.
Activity
mortb commentedon Jun 29, 2016
If you can't wait for the team to answer, here is a workaround:
sandersn commentedon Jun 29, 2016
I don't think TypeScript can help in this case because
switchworking with arrays would be quite different from JavaScript's behaviour. You probably should support the ECMAScript pattern matching proposal. If that's accepted into the standard, you will get tuple matching and more.frogcjn commentedon Jun 29, 2016
Nathan Shively-Sanders (@sandersn)
does ECMAScript have tuple?
I did not proposal array match, but tuple.
If the programming language has a "tuple" concept, it means it can match with other tuple.
There is nothing to do with array, though you use array to simulate tuple.
sandersn commentedon Jun 29, 2016
You just need array matching to get the semantics you want. Tuples are just a type to make sure that you aren't matching against nonsensical patterns.
Of course if ES20xx adds support for pattern matching, then TypeScript will allow you to type arrays as tuple in a matching context.
frogcjn commentedon Jun 29, 2016
Martin Böh (@MartB), thanks. I solved it what as you writes.
But as I suggest, typescript has a "tuple" concept, then it should has an ability to match tuple in switch.
frogcjn commentedon Jun 29, 2016
Nathan Shively-Sanders (@sandersn) OK. I'll proposal ECMAScript has all type features like typescript. And then I'll wait until 2019.
sandersn commentedon Jun 29, 2016
Pattern matching isn't really a type-system concept. There are plenty of languages with pattern matching but no compile-time types. (Erlang, Clojure, Common Lisp and Scheme come to mind immediately.)
kitsonk commentedon Jun 29, 2016
JavaScript does not
switchon non-primatives (references). You cannotswitchon Objects, Arrays, etc..While a tuple type is a TypeScript concept, it is still a non-primative concept. As tuples stand at the moment, they are a design time only concept, with their typing information fully erasable by using Array literals at run-time. Implementing this feature it would break several of the TypeScript Design Goals:
And would be an anti-goal: