“Computed” property in Typescript
up vote
0
down vote
favorite
Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.
Imagine I have a class/interface/type - e.g. Person
- with a few properties, that are returned by a Web API call - something like this:
export interface Person {
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question
}
What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.
In C#, I would just create a property
string FullName { get { return $"{FirstName} {LastName}"; } }
and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?
c# angular typescript properties
add a comment |
up vote
0
down vote
favorite
Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.
Imagine I have a class/interface/type - e.g. Person
- with a few properties, that are returned by a Web API call - something like this:
export interface Person {
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question
}
What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.
In C#, I would just create a property
string FullName { get { return $"{FirstName} {LastName}"; } }
and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?
c# angular typescript properties
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 at 12:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.
Imagine I have a class/interface/type - e.g. Person
- with a few properties, that are returned by a Web API call - something like this:
export interface Person {
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question
}
What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.
In C#, I would just create a property
string FullName { get { return $"{FirstName} {LastName}"; } }
and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?
c# angular typescript properties
Folks, I'm in the middle of "learning by doing" in Angular 6 & 7, and I came across this issue a number of times.
Imagine I have a class/interface/type - e.g. Person
- with a few properties, that are returned by a Web API call - something like this:
export interface Person {
FirstName: string;
LastName: string;
JobTitle: string;
// so other properties - not relevant to this question
}
What I'd like to be able to is show the full name (e.g. "FirstName + [Space] + LastName") in e.g. an Angular grid (AG-Grid) or someplace else - where I cannot use a concat expression or anything, but I need to refer to a single property on the class/interface/type.
In C#, I would just create a property
string FullName { get { return $"{FirstName} {LastName}"; } }
and be done with it - but how can I do this in Typescript?? From what I've been reading and researching, this seems to be unsupported - really?!?!!? How can that be??? Seems like such a simple and often-used operation - what's the reason this doesn't exist in Typescript?? Or does it exist - and I just haven't found the way to do this in TS?
c# angular typescript properties
c# angular typescript properties
asked Nov 10 at 12:19
marc_s
564k12510881241
564k12510881241
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 at 12:25
add a comment |
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 at 12:25
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 at 12:25
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 at 12:25
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.
BTW members in TypeScript use camelCase
:
export interface Person {
firstName: string;
lastName : string;
jobTitle : string;
fullName : string;
}
class SimplePerson implements Person {
// ...
get fullName(): string {
return this.firstName + " " + this.lastName;
}
}
add a comment |
up vote
0
down vote
Javascript supports get
and set
when defining a property (mostly using Object.defineProperty
).
Apparently there's an handy syntax for it in typescript (for classes) :
class MyClass{
firstName: string;
lastName: string;
constructor(firstName: string, lastName: string){
this.firstName = firstName;
this.lastName = lastName;
}
get fullName(){
return `${this.firstName} ${this.lastName}`;
}
}
Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.
add a comment |
up vote
0
down vote
You can also define get
ters and set
ters in JavaScript.
Try this in your Component Class:
person: Person;
...
// You got the person Object from your Backend API.
...
// Now
get fullName() {
return `${this.person.firstName} ${this.person.lastName}`;
}
And then in your Template:
Simply use fullName
like this:
<p>{{ fullName }}</p>
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.
BTW members in TypeScript use camelCase
:
export interface Person {
firstName: string;
lastName : string;
jobTitle : string;
fullName : string;
}
class SimplePerson implements Person {
// ...
get fullName(): string {
return this.firstName + " " + this.lastName;
}
}
add a comment |
up vote
2
down vote
If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.
BTW members in TypeScript use camelCase
:
export interface Person {
firstName: string;
lastName : string;
jobTitle : string;
fullName : string;
}
class SimplePerson implements Person {
// ...
get fullName(): string {
return this.firstName + " " + this.lastName;
}
}
add a comment |
up vote
2
down vote
up vote
2
down vote
If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.
BTW members in TypeScript use camelCase
:
export interface Person {
firstName: string;
lastName : string;
jobTitle : string;
fullName : string;
}
class SimplePerson implements Person {
// ...
get fullName(): string {
return this.firstName + " " + this.lastName;
}
}
If it's an interface then there's no syntax, because all properties in JavaScript can have getter/setter functions instead of being exposed fields. It's an implementation concern.
BTW members in TypeScript use camelCase
:
export interface Person {
firstName: string;
lastName : string;
jobTitle : string;
fullName : string;
}
class SimplePerson implements Person {
// ...
get fullName(): string {
return this.firstName + " " + this.lastName;
}
}
answered Nov 10 at 12:26
Dai
71.1k13111200
71.1k13111200
add a comment |
add a comment |
up vote
0
down vote
Javascript supports get
and set
when defining a property (mostly using Object.defineProperty
).
Apparently there's an handy syntax for it in typescript (for classes) :
class MyClass{
firstName: string;
lastName: string;
constructor(firstName: string, lastName: string){
this.firstName = firstName;
this.lastName = lastName;
}
get fullName(){
return `${this.firstName} ${this.lastName}`;
}
}
Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.
add a comment |
up vote
0
down vote
Javascript supports get
and set
when defining a property (mostly using Object.defineProperty
).
Apparently there's an handy syntax for it in typescript (for classes) :
class MyClass{
firstName: string;
lastName: string;
constructor(firstName: string, lastName: string){
this.firstName = firstName;
this.lastName = lastName;
}
get fullName(){
return `${this.firstName} ${this.lastName}`;
}
}
Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.
add a comment |
up vote
0
down vote
up vote
0
down vote
Javascript supports get
and set
when defining a property (mostly using Object.defineProperty
).
Apparently there's an handy syntax for it in typescript (for classes) :
class MyClass{
firstName: string;
lastName: string;
constructor(firstName: string, lastName: string){
this.firstName = firstName;
this.lastName = lastName;
}
get fullName(){
return `${this.firstName} ${this.lastName}`;
}
}
Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.
Javascript supports get
and set
when defining a property (mostly using Object.defineProperty
).
Apparently there's an handy syntax for it in typescript (for classes) :
class MyClass{
firstName: string;
lastName: string;
constructor(firstName: string, lastName: string){
this.firstName = firstName;
this.lastName = lastName;
}
get fullName(){
return `${this.firstName} ${this.lastName}`;
}
}
Regarding interfaces, I'm not sure such a thing is possible since their sole purpose is data description not implementation.
answered Nov 10 at 12:28
Vivick
1,1951317
1,1951317
add a comment |
add a comment |
up vote
0
down vote
You can also define get
ters and set
ters in JavaScript.
Try this in your Component Class:
person: Person;
...
// You got the person Object from your Backend API.
...
// Now
get fullName() {
return `${this.person.firstName} ${this.person.lastName}`;
}
And then in your Template:
Simply use fullName
like this:
<p>{{ fullName }}</p>
add a comment |
up vote
0
down vote
You can also define get
ters and set
ters in JavaScript.
Try this in your Component Class:
person: Person;
...
// You got the person Object from your Backend API.
...
// Now
get fullName() {
return `${this.person.firstName} ${this.person.lastName}`;
}
And then in your Template:
Simply use fullName
like this:
<p>{{ fullName }}</p>
add a comment |
up vote
0
down vote
up vote
0
down vote
You can also define get
ters and set
ters in JavaScript.
Try this in your Component Class:
person: Person;
...
// You got the person Object from your Backend API.
...
// Now
get fullName() {
return `${this.person.firstName} ${this.person.lastName}`;
}
And then in your Template:
Simply use fullName
like this:
<p>{{ fullName }}</p>
You can also define get
ters and set
ters in JavaScript.
Try this in your Component Class:
person: Person;
...
// You got the person Object from your Backend API.
...
// Now
get fullName() {
return `${this.person.firstName} ${this.person.lastName}`;
}
And then in your Template:
Simply use fullName
like this:
<p>{{ fullName }}</p>
edited Nov 10 at 12:36
answered Nov 10 at 12:26
SiddAjmera
9,01621037
9,01621037
add a comment |
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238881%2fcomputed-property-in-typescript%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 at 12:25