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

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

Open
TheCelavi opened this issue Aug 12, 2021 · 2 comments

Comments

@TheCelavi
Copy link

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!

@orta
Copy link
Contributor

orta commented Aug 12, 2021

TypeScript's design goals are to not make changes like this: https://www.youtube.com/watch?v=8qm49TyMUPI&t=1s

@TheCelavi
Copy link
Author

Ok, that is 30 minutes of video, if you had time to record it, I will find time to watch it in full, thank you.

In general, I understand your point.

But isn't a "readonly" for class property a syntactic sugar added by TypeScript, adding more than just plain strict typings? Seams that adding support for "readonly" variables is kinda next natural step forward?

@TheCelavi TheCelavi changed the title [ FEATURE REQUEST ] TypeScript should fix broken "const" variable declaration in JavaScript [ FEATURE REQUEST ] TypeScript could fix broken "const" variable declaration in JavaScript Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants