Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index signature initialization #34760

Closed
ghost opened this issue Oct 28, 2019 · 2 comments
Closed

Index signature initialization #34760

ghost opened this issue Oct 28, 2019 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ghost
Copy link

ghost commented Oct 28, 2019

TypeScript Version: 3.5.3

Code

export class User {
  id: number = 0

  [key: string]: any
}

Expected behavior:

Should work like

export class User {
  id: number = 0; // <- semicolon

  [key: string]: any
}
export class User {
  id: number // no initialization

  [key: string]: any
}

Actual behavior:

error TS1005: ']' expected.

Playground Link:
https://www.typescriptlang.org/play/#code/KYDwDg9gTgLgBAYwDYEMDOa4FU3CnAbwCg44BLAEwC44A7AVwFsAjPOAXjgAYiS4BtANbAAnjTQwoZWgHMAujRS0RRAL5A

CC prettier/prettier#6727 , prettier/prettier#6728

@IllusionMH
Copy link
Contributor

Looks like this is same problem as with class fields proposal tc39/proposal-class-fields#7.
ASI won't insert ; if next line starts with [ and therefore your code is parsed as indexed access id: number = 0[key which leads to an error, because : won't be part of valid expression for indexed access.

See proposal source

Not sure if TS can easily avoid this requirement even if [ is part of the type and not computed property.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 30, 2019
@RyanCavanaugh
Copy link
Member

ASI hazard is hazardous. We don't want to do a complex lookahead here on [ to figure out if it's an index signature or element access expression (I'm not even 100% convinced it's never ambiguous).

Line-leading [ should always have an associated ; in desemicoloned style

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants