Skip to content

"'const' declarations can only be declared inside a block" should be issued more aggressivelyΒ #61734

@RyanCavanaugh

Description

@RyanCavanaugh

πŸ”Ž Search Terms

const let block keyword if

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABMAFASkQbwFCMTYRFARkQD5EAGNXPRCBAZykQFNEBeRAZgG5a8DMIzgAbVgDpRcAOYpWafngC+2VUA

πŸ’» Code

This code issues a somewhat perplexing error

function f() {
  if (1 > 0)
    const e = 3;
    console.log(e);
  }
}

Image

While there's also an error on the final }, this can easily be off-screen, so it appears like TS has totally gone off the rails. We should issue the correct "const' declarations can only be declared inside a block" error more-unconditionally so that it's apparent that there's a syntax error earlier in the program (the missing { after the if)

πŸ™ Actual behavior

The error doesn't appear until all the other syntactic problems have been removed

πŸ™‚ Expected behavior

It should always appear

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions