Typescript interface with private members
up vote
1
down vote
favorite
Why typescript not supporting private members in interfaces?
How to handle the following scenario?
interface IFoo
{
private member: {};
}
class Foo implements IFoo
{
private member = {};
}
typescript
add a comment |
up vote
1
down vote
favorite
Why typescript not supporting private members in interfaces?
How to handle the following scenario?
interface IFoo
{
private member: {};
}
class Foo implements IFoo
{
private member = {};
}
typescript
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Why typescript not supporting private members in interfaces?
How to handle the following scenario?
interface IFoo
{
private member: {};
}
class Foo implements IFoo
{
private member = {};
}
typescript
Why typescript not supporting private members in interfaces?
How to handle the following scenario?
interface IFoo
{
private member: {};
}
class Foo implements IFoo
{
private member = {};
}
typescript
typescript
asked Nov 21 at 22:46
Ahmed M.Kamal
158212
158212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Interfaces define "public contracts", it describes the public side of the class and as such it doesn't make sense to have private access modifier.
From Typescript interface documentation:
This prohibits you from using them to check that a class also has
particular types for the private side of the class instance.
But an interface can extend classes with private memmbers.
From Understanding TypeScript:
In TypeScript, interfaces can also extend classes, but only in a way
that involves inheritance. When an interface extends a class, the
interface includes all class members (public and private), but without
the class’ implementations.
class Customer
{
private id: number;
get Id(): number
{
return this.id
}
set Id( value: number )
{
this.id = value;
}
}
interface ICustomer extends Customer
{
MiddleName: string;
}
Thanks for explanation, but isn't this considered a weakness of the language?
– Ahmed M.Kamal
Nov 21 at 23:20
1
@AhmedM.Kamal in what way? It makes very little sense to check if the instance itself implements something, you can see it yourself right in your editor already.
– zerkms
Nov 21 at 23:21
@zerkms it makes sense for me now, thanks.
– Ahmed M.Kamal
Nov 21 at 23:34
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Interfaces define "public contracts", it describes the public side of the class and as such it doesn't make sense to have private access modifier.
From Typescript interface documentation:
This prohibits you from using them to check that a class also has
particular types for the private side of the class instance.
But an interface can extend classes with private memmbers.
From Understanding TypeScript:
In TypeScript, interfaces can also extend classes, but only in a way
that involves inheritance. When an interface extends a class, the
interface includes all class members (public and private), but without
the class’ implementations.
class Customer
{
private id: number;
get Id(): number
{
return this.id
}
set Id( value: number )
{
this.id = value;
}
}
interface ICustomer extends Customer
{
MiddleName: string;
}
Thanks for explanation, but isn't this considered a weakness of the language?
– Ahmed M.Kamal
Nov 21 at 23:20
1
@AhmedM.Kamal in what way? It makes very little sense to check if the instance itself implements something, you can see it yourself right in your editor already.
– zerkms
Nov 21 at 23:21
@zerkms it makes sense for me now, thanks.
– Ahmed M.Kamal
Nov 21 at 23:34
add a comment |
up vote
1
down vote
Interfaces define "public contracts", it describes the public side of the class and as such it doesn't make sense to have private access modifier.
From Typescript interface documentation:
This prohibits you from using them to check that a class also has
particular types for the private side of the class instance.
But an interface can extend classes with private memmbers.
From Understanding TypeScript:
In TypeScript, interfaces can also extend classes, but only in a way
that involves inheritance. When an interface extends a class, the
interface includes all class members (public and private), but without
the class’ implementations.
class Customer
{
private id: number;
get Id(): number
{
return this.id
}
set Id( value: number )
{
this.id = value;
}
}
interface ICustomer extends Customer
{
MiddleName: string;
}
Thanks for explanation, but isn't this considered a weakness of the language?
– Ahmed M.Kamal
Nov 21 at 23:20
1
@AhmedM.Kamal in what way? It makes very little sense to check if the instance itself implements something, you can see it yourself right in your editor already.
– zerkms
Nov 21 at 23:21
@zerkms it makes sense for me now, thanks.
– Ahmed M.Kamal
Nov 21 at 23:34
add a comment |
up vote
1
down vote
up vote
1
down vote
Interfaces define "public contracts", it describes the public side of the class and as such it doesn't make sense to have private access modifier.
From Typescript interface documentation:
This prohibits you from using them to check that a class also has
particular types for the private side of the class instance.
But an interface can extend classes with private memmbers.
From Understanding TypeScript:
In TypeScript, interfaces can also extend classes, but only in a way
that involves inheritance. When an interface extends a class, the
interface includes all class members (public and private), but without
the class’ implementations.
class Customer
{
private id: number;
get Id(): number
{
return this.id
}
set Id( value: number )
{
this.id = value;
}
}
interface ICustomer extends Customer
{
MiddleName: string;
}
Interfaces define "public contracts", it describes the public side of the class and as such it doesn't make sense to have private access modifier.
From Typescript interface documentation:
This prohibits you from using them to check that a class also has
particular types for the private side of the class instance.
But an interface can extend classes with private memmbers.
From Understanding TypeScript:
In TypeScript, interfaces can also extend classes, but only in a way
that involves inheritance. When an interface extends a class, the
interface includes all class members (public and private), but without
the class’ implementations.
class Customer
{
private id: number;
get Id(): number
{
return this.id
}
set Id( value: number )
{
this.id = value;
}
}
interface ICustomer extends Customer
{
MiddleName: string;
}
edited Nov 21 at 23:23
answered Nov 21 at 23:11
Maarti
1,0481721
1,0481721
Thanks for explanation, but isn't this considered a weakness of the language?
– Ahmed M.Kamal
Nov 21 at 23:20
1
@AhmedM.Kamal in what way? It makes very little sense to check if the instance itself implements something, you can see it yourself right in your editor already.
– zerkms
Nov 21 at 23:21
@zerkms it makes sense for me now, thanks.
– Ahmed M.Kamal
Nov 21 at 23:34
add a comment |
Thanks for explanation, but isn't this considered a weakness of the language?
– Ahmed M.Kamal
Nov 21 at 23:20
1
@AhmedM.Kamal in what way? It makes very little sense to check if the instance itself implements something, you can see it yourself right in your editor already.
– zerkms
Nov 21 at 23:21
@zerkms it makes sense for me now, thanks.
– Ahmed M.Kamal
Nov 21 at 23:34
Thanks for explanation, but isn't this considered a weakness of the language?
– Ahmed M.Kamal
Nov 21 at 23:20
Thanks for explanation, but isn't this considered a weakness of the language?
– Ahmed M.Kamal
Nov 21 at 23:20
1
1
@AhmedM.Kamal in what way? It makes very little sense to check if the instance itself implements something, you can see it yourself right in your editor already.
– zerkms
Nov 21 at 23:21
@AhmedM.Kamal in what way? It makes very little sense to check if the instance itself implements something, you can see it yourself right in your editor already.
– zerkms
Nov 21 at 23:21
@zerkms it makes sense for me now, thanks.
– Ahmed M.Kamal
Nov 21 at 23:34
@zerkms it makes sense for me now, thanks.
– Ahmed M.Kamal
Nov 21 at 23:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421494%2ftypescript-interface-with-private-members%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown