1

Why this code produce errors?

let promises = [p1(), p2(), p3()];
$q.all(promises)
  .then(([p1Result, p2Result, p3Result]) => {
    // ...
  });

But when I turn it to this, it works:

let promises = [p1(), p2(), p3()];
$q.all(promises)
  .then(results => {
    let [p1Result, p2Result, p3Result] = results;
    // ...
  });

Another problem I have is that I can't define a type for p1Result, p2Result, p3Result. Not to mention that Typescript compiler does not infer types of them.

2
  • What do you get in the output for both code snippets? Commented Oct 27, 2015 at 14:05
  • 1
    Ideally both should just work fine. And you can define the type as [p1Result, p2Result, p3Result]:[p1Type, p2Type, p3Type] or even results:[p1Type, p2Type, p3Type]. What is the error you see? Commented Oct 27, 2015 at 14:46

1 Answer 1

1

They both work the same

TypeScript

declare var $q: any;
declare var p1: any;
declare var p2: any;
declare var p3: any;
let promises = [p1(), p2(), p3()];


$q.all(promises)
  .then(([p1Result, p2Result, p3Result]) => {
    // ...
  });

$q.all(promises)
  .then(results => {
    let [p1Result, p2Result, p3Result] = results;
    // ...
  });

Generated JavaScript:

var promises = [p1(), p2(), p3()];
$q.all(promises)
    .then(function (_a) {
    var p1Result = _a[0], p2Result = _a[1], p3Result = _a[2];
    // ...
});
$q.all(promises)
    .then(function (results) {
    var p1Result = results[0], p2Result = results[1], p3Result = results[2];
    // ...
});

You can see that TypeScript is happy with both code samples

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I think it is a bug in VS 2015. Your code gets intellisense error in VS but compiles successfully. I was relying on intellisense.
I am using it and it gives me the best experience working with typescript, but because of other parts of code in that project we were using VS. Actually I was thinking about this today ;)
Actually, It was Resharper's bug!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.