basarat / basarat/typescript-book

[ FEATURE REQUEST ] TypeScript could fix broken "const" variable declaration in JavaScript

Open
#647 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
21.6k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

In all programming languages, "const" means "a design time constant" which is immutable. But not for JavaScript, it is a variable that is a readonly within context. It is a "readonly" - not a constant.

Type script should easily "fix" this allowing us to use "readonly" for variable declaration.

Wrong:

```
(a: number) => {
const b: number = a || 1; // this is not a constant, this is readonly variable
}

Good:

(a: number) => {
readonly b: number = a || 1; // this makes sense
}
```

And transpiler should be able to easily just rename from "readonly b" to "const b" in JavaScript version of the code.

I as a developer with knowledge of other programming languages have so much issue with abuse of "const" keyword in JavaScript => and TypeScript, yet again has an opportunity to fix broken language.

Pretty please?

Thank you!

Contributor guide

Open the contributing guide

Research direction

No files, tests, or entry points are mentioned. Start by determining whether this TypeScript syntax proposal belongs in this book repository or the TypeScript compiler, then review how TypeScript parses readonly declarations and emits const. Done would require an accepted design and matching parser, type-checker, and transpiler behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.