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

Comments

Projects
None yet
3 participants
@terbooter

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

This comment has been minimized.

Show comment
Hide comment
@kitsonk

kitsonk Aug 24, 2016

Contributor

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);
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

This comment has been minimized.

Show comment
Hide comment
@RyanCavanaugh

RyanCavanaugh Aug 24, 2016

Member

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

Member

RyanCavanaugh commented Aug 24, 2016

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

@terbooter

This comment has been minimized.

Show comment
Hide comment
@terbooter

terbooter Aug 24, 2016

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

terbooter commented Aug 24, 2016

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

@terbooter

This comment has been minimized.

Show comment
Hide comment
@terbooter

terbooter Aug 24, 2016

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

terbooter commented Aug 24, 2016

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

This comment has been minimized.

Show comment
Hide comment
@kitsonk

kitsonk Aug 24, 2016

Contributor

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.

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

This comment has been minimized.

Show comment
Hide comment
@RyanCavanaugh

RyanCavanaugh Aug 24, 2016

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.

Member

RyanCavanaugh commented Aug 24, 2016

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

This comment has been minimized.

Show comment
Hide comment
@terbooter

terbooter Aug 24, 2016

Thanks for your answers.

terbooter commented Aug 24, 2016

Thanks for your answers.

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