Reputation: 1521
I have a function that returns a tuple [number, number, number]
. I want to use destructuring to assign to my class variables the values returned by the tuple like this:
let [this.a, this.b, this.c] = functionThatReturnsTupleWithThreeNumbers()
but this doesn't work.
Upvotes: 3
Views: 213
Reputation: 37918
Just remove the let
and you're good to go:
class Foo {
a;
b;
c;
bar() {
[this.a, this.b, this.c] = [1, 2, 3];
}
}
const foo = new Foo();
foo.bar();
console.log(foo);
More info here
Typescript playground
Upvotes: 2
Reputation: 1073
You need to have the props in your class and destruct your tuple into them:
class MyClass {
a: number;
b: number;
c: number;
constructor(tuple: ReadonlyArray<number>) {
[this.a, this.b, this.c] = tuple;
}
}
Upvotes: 1