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

TS allow access private class field without error #10516

Closed
terbooter opened this issue Aug 24, 2016 · 7 comments
Closed

TS allow access private class field without error #10516

terbooter opened this issue Aug 24, 2016 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@terbooter
Copy link

terbooter commented Aug 24, 2016

TypeScript Version:

$ tsc -v
Version 1.8.10

Create file app.ts with following content:

class User {
    constructor(private name:string) {

    }

    public hello(who:User) {
        // name is the private property of object 'who'. TS should show error like
        // "Cat access private field 'name'"
        console.log("Hello, " + who.name);
    }
}

var vasya = new User("VASYA");
var peter = new User("PETER");
vasya.hello(peter);

Compile this file

tsc app.ts

Actual behavior:
You will see no error.

Expected behavior:
It should show error
"Cat access private field 'name'"

@kitsonk
Copy link
Contributor

kitsonk commented Aug 24, 2016

BTW, This only occurs when the classes are the same:

class Foo {
    constructor(private name: string) {

    }
}

class User {
    constructor(private name:string) {

    }

    public hello(who: Foo) {
        console.log("Hello, " + who.name); // Property 'name' is private and is only accessible from within class 'Foo'
    }
}

var vasya = new User("VASYA");
var peter = new Foo("PETER");
vasya.hello(peter);

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 24, 2016
@RyanCavanaugh
Copy link
Member

Like in most other languages, classes may access private members of other instances of the same class.

@terbooter
Copy link
Author

@RyanCavanaugh could you please give examples of such languages.
Such behaviour is very surprising for me.

@terbooter
Copy link
Author

I think name field should be protected to access it from same class
@ahejlsberg said that

Protected members can be accessed only within the declaring class and subclasses of the declaring class.

#688

@kitsonk
Copy link
Contributor

kitsonk commented Aug 24, 2016

That would still apply... User is the declaring class. The visibility modifiers are at the class level, not the instance level.

@terbooter there is PHP and C# and Java and pretty much every other OO language.

@RyanCavanaugh
Copy link
Member

The only language I'm aware of that uses instance-based visibility instead of type-based visibility is Ruby. Java, C#, C++, Swift, PHP, etc. all allow access to other instances' private members.

@terbooter
Copy link
Author

Thanks for your answers.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants