Skip to content

Conversation

@ahejlsberg
Copy link
Member

With this PR a parameter is implicitly considered const when there are no assignments to the parameter anywhere in the declaring function (or functions nested within it). Similar to const variables, when a parameter is considered const, type guards for the parameter remain in effect in nested function expressions and arrow functions:

function f1(x: Object) {
    if (typeof x === "string") {
        setTimeout(() => console.log(x.charAt(0)), 0);
    }
}

function f2(info?: { name: string }) {
    if (!info) {
        return;
    }
    setTimeout(() => console.log(info.name), 0);
}

Above, x and info are implicitly const because there are no assignments to them anywhere in their containing functions, and therefore the type guards remain in effect in the callback functions.

Fixes #10180. Also see #7719 and a number of similar issues.

/* @internal */ exportSymbol?: Symbol; // Exported symbol associated with this symbol
/* @internal */ constEnumOnlyModule?: boolean; // True if module contains only const enums or other modules with only const enums
/* @internal */ isReferenced?: boolean; // True if the symbol is referenced elsewhere
/* @internal */ isAssigned?: boolean; // True if the symbol has assignments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarify that this flag is only set for parameters

@RyanCavanaugh
Copy link
Member

👍 . Seems like Travis isn't going anywhere...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants