Skip to content

Private members #41773

Closed
Closed

Description

@qwabra

TypeScript Version: 4.1.2

Search Terms:

Expected behavior:

should throw an error with "target": "ESNext" and # private member

Actual behavior:

Related Issues:

#17535
https://stackoverflow.com/a/45244695/4794368

Code

class A {
    #str = 'pr Str'
    constructor() {
        new A.B(this)
    }
    private static B = class {
        constructor(private a: A) {
            console.log(this.a.#str)
        }
    }
}
test: {
    new A
}
Output
"use strict";
class A {
    constructor() {
        this.#str = 'pr Str';
        new A.B(this);
    }
    #str;
}
A.B = class {
    constructor(a) {
        this.a = a;
        console.log(this.a.#str);
    }
};
test: {
    new A;
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "allowUnusedLabels": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "Latest",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

Activity

  1. RyanCavanaugh commented on Dec 2, 2020

    @RyanCavanaugh
    Member

    Why? This code runs without error

    function f() {
        "use strict";
        class A {
            #str = 'pr Str'
            constructor() {
                new A.B(this)
            }
            static B = class {
                constructor(a) {
                    console.log(a.#str)
                }
            }
        }
    
        new A();
    }

    image

  2. qwabra commented on Dec 2, 2020

    @qwabra
    Author

    Oh, hmm, hmmm but.

    But your example is different from the compiler output

  3. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    Needs More InfoThe issue still hasn't been fully clarified
    on Dec 2, 2020
  4. RyanCavanaugh commented on Dec 2, 2020

    @RyanCavanaugh
    Member

    Not really seeing any argument being made here. Private identifiers have lexical visibility and #str is in B's containing lexical scope.

  5. qwabra commented on Dec 2, 2020

    @qwabra
    Author

    ok, now i see. but the generated code doesn't work. please look at the output

  6. DanielRosenwasser commented on Dec 2, 2020

    @DanielRosenwasser
    Member

    Thanks for clarifying - I opened up #41788 for you.

  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions