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

fix(42734): TS18030: An optional chain cannot contain private identifiers #42743

Closed
wants to merge 1 commit into from

Conversation

a-tarasyuk
Copy link
Contributor

Fixes #42734

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Feb 10, 2021
@dragomirtitian
Copy link
Contributor

@a-tarasyuk I looked at this issue too but got stuck on emit changes. The private field transform will now just remove the ?. checks:

This:

class X {
    #test = 0;
    m() {
        let a: X | undefined = undefined;
        a?.#test
    }
}

Transforms to this:

class X {
    constructor() {
        _test.set(this, 0);
    }
    m() {
        let a = undefined;
        __classPrivateFieldGet(a, _test);
    }
}

which will result in a Type error.
Playground Link

@a-tarasyuk a-tarasyuk marked this pull request as draft February 11, 2021 09:18
@a-tarasyuk
Copy link
Contributor Author

It looks like classFields doesn't support OptionalChain transformation.

function visitPropertyAccessExpression(node: PropertyAccessExpression) {
if (shouldTransformPrivateFields && isPrivateIdentifier(node.name)) {
const privateIdentifierInfo = accessPrivateIdentifier(node.name);
if (privateIdentifierInfo) {
return setOriginalNode(
createPrivateIdentifierAccess(privateIdentifierInfo, node.expression),
node
);
}
}
return visitEachChild(node, visitor, context);

@rbuckton Can we share OptionalChain transformation from es2020 with the classFields?

lgarron added a commit to cubing/cubing.js that referenced this pull request May 8, 2021
We can't quite use it for everything until `?.#` is supported, which may be in TypeScript 4.3.1: microsoft/TypeScript#42743
@a-tarasyuk a-tarasyuk closed this Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TS18030: An optional chain cannot contain private identifiers
4 participants