question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New Feature Requesting for `const` Parameter

See original GitHub issue

I 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:open
  • Created 6 years ago
  • Reactions:118
  • Comments:39 (12 by maintainers)

github_iconTop GitHub Comments

56reactions
brncommented, Sep 17, 2017

I think better to use readonly keyword for this proposal.

function foo(readonly constantProperty: any) {
  constantProperty = 1 // Error. Couldn't assign a readonly parameter.
}
46reactions
ghostcommented, Jul 6, 2019

I really want this

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found