Tuple types

Topics: Language Specification
Oct 2, 2012 at 8:28 AM
Edited Oct 2, 2012 at 8:33 AM

The only javascript pattern I've not been able to express in the type system are tuple return values. Since js obviously lacks a tuple type, these are simply fixed length arrays with types at known locations. The pattern isn't popular; I've mostly seen the pattern in parsers but with the destructuring strawman accepted for ES6, I expect the usage to increase.

Simple example (it doesn't make a lot of sense not to add the type to the object...):


function tokenize (source) {
  // ...
  return ['binaryOp', {op: '+', left: '2', right: '2'}];
  // ...


Trying to express the type signature of the function in obvious ways doesn't work:


(string) => [string, Token]     // first try
(string) => {0:string, 1:token} // second try

From reading the language spec, it looks like the first is being used for type annotating [] property access for number vs string and the second fails because 0 and 1 aren't getting parsed as valid identifiers, nor are strings.


I appreciate you working on this and open sourcing it permissively. I've been interested in providing quality completion for JS for a while and from a quick scan it looks like I might be able to get it from what's there plus some appropriate .d.ts annotations.

Oct 5, 2012 at 8:29 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.