New Feature Requesting for `const` Parameter
See original GitHub issueI have a small new feature to request. Let say we have this piece of code:
function setButton(button: Button | undefined) {
if (button) {
button.onclick = () => {
// tsc complains button might be undefined(well done!),
// because it might be set to undefined later on,
// as you can see below `button = undefined;`.
button.setAttribute('disabled', 'true'); //error
}
}
// some other code goes here...
// and eventually, button is set to undefined.
button = undefined;
}
Fortunately, there is a way to fix this:
function setButton(button: Button | undefined) {
const constButton = button;
if (constButton) {
constButton.onclick = () => {
// now tsc is satisfied.
constButton.setAttribute('disabled', 'true');
}
}
// impossible to assign to a const variable.
// constButton = undefined;
}
But can we twist the code a little bit, which is what I asking for? Something like this:
function setButton(const button: Button | undefined) {
if (button) {
button.onclick = () => {
button.setAttribute('disabled', 'true');
}
}
}
So you can see there really are some cases a const
parameter might come to handy. Thank you.
P.S. Just for demonstrating the point, here’s a full demo:
class Button {
onclick?: () => void;
performClick() {
if (this.onclick) this.onclick();
}
setAttribute(key: string, value: string) {
//set attribute to this button
}
}
function setButton(button: Button | undefined) {
if (button) {
button.onclick = () => {
button.setAttribute('disabled', 'true');
}
}
button = undefined;
}
const button = new Button();
setButton(button);
button.performClick();
Issue Analytics
- State:
- Created 6 years ago
- Reactions:118
- Comments:39 (12 by maintainers)
Top Results From Across the Web
Use of 'const' for function parameters - c++ - Stack Overflow
I've voted this one up. Declaring the parameter 'const' adds semantic information to the parameter. They highlight what the original author of ...
Read more >const - JavaScript - MDN Web Docs - Mozilla
The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable ......
Read more >Function arguments - Manual - PHP
Default parameter values may be scalar values, arrays, the special type null , and as of PHP 8.1.0, objects using the new ClassName()...
Read more >Twilio Function Execution
The event object contains the request parameters and headers being passed ... a new messaging response object; no need to import Twilio const...
Read more >Documentation - More on Functions - TypeScript
The function type (string) => void means “a function with a parameter named string of ... JavaScript functions can also be invoked with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think better to use
readonly
keyword for this proposal.I really want this