Syntax Error on Destructuring Assignment in TypeScript

dbld
interface User extends Function {
    player: number,
    units: number[],
    sites: string[],
}
class User extends Function {
    constructor() {
        super('return this.player')
        [this.player, this.units, this.sites] = getBelongings(); // Destructuring Assignment
        
    }
}
const me = new User();
function getBelongings(): [number, number[], string[]] {
    return [1, [1], ['1']]
}

Playground Link

The above seems nice, and I think my assignment has no problem.

But It says: Type 'string[]' cannot be used as an index type.(2538)

I tried destructuring some times before and now have no idea about what's wrong.

Is it a TypeScript problem or a wrong syntax? And how can I make it operate well? Thanks a lot.

Dave Meehan

You need a semi colon after the call to super, or it's trying to run both statements together.

The code, as it stands, reads as an index accessor to the return value from super, instead of a new statement.

super('return this.player');   // <-- insert semi colon
[this.player, this.units, this.sites] = getBelongings(); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

typescript destructuring assignment with interface

Destructuring assignment in Typescript

Is there a syntax only for destructuring assignment in Svelte?

JS/TS syntax: Destructuring assignment with inner assignment

Destructuring assignment in Typescript for global variables

Destructuring assignment via TypeScript in React

Using destructuring assignment and the rest syntax for default variables

Must use destructuring props assignment error in constructor

Destructuring assignment cause error: "unexpected token ="

Error: Must use destructuring state assignment

Destructuring with spread syntax in reduce method using typescript

How to use special characters (like hyphen) in destructuring assignment syntax?

Reassign variables using ES6 destructuring assignment syntax

Typescript Object destructuring results in "Property assignment expected."

TypeScript type modification when using defaults and object destructuring assignment

Property does not exists on type when destructuring assignment and 'Pick' in Typescript

Python Assignment Expressions syntax error

Syntax error eval'ing an assignment

Cannot find error while destructuring arguments in TypeScript

typescript error when I tried to destructuring an object

Typescript 2.9 error with dynamic assignment

Property assignment expected error on TypeScript

How to namespace Destructuring assignment?

Is this an example of destructuring assignment?

Destructuring assignment, but without a pattern

Python assignment destructuring

Object property assignment with destructuring?

Destructuring assignment for nested properties

Destructuring assignment and variable swapping