“Computed” property in Typescript
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 |
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 '18 at 12:25
add a comment |
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 '18 at 12:19
marc_s
571k12811031252
571k12811031252
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 '18 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 '18 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 '18 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 '18 at 12:25
add a comment |
4 Answers
4
active
oldest
votes
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 |
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 |
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 |
Could try using an array of string;
var fullName: Array<string> = Array<string>();
var firstName: string = 'Bob';
var lastName: string = 'Boberanne';
fullName.push(firstName);
fullName.push(lastName);
In html:
<p>fullName.join(' ')</p>
<p>[fistname, lastName].join(' ')</p>
In typescript:
fullName.join(' ');
[firstName, lastName].join(' ');
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f53238881%2fcomputed-property-in-typescript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 12:26
Dai
72k13113201
72k13113201
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:28
Vivick
1,3411417
1,3411417
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:36
answered Nov 10 '18 at 12:26
SiddAjmera
13.1k31137
13.1k31137
add a comment |
add a comment |
Could try using an array of string;
var fullName: Array<string> = Array<string>();
var firstName: string = 'Bob';
var lastName: string = 'Boberanne';
fullName.push(firstName);
fullName.push(lastName);
In html:
<p>fullName.join(' ')</p>
<p>[fistname, lastName].join(' ')</p>
In typescript:
fullName.join(' ');
[firstName, lastName].join(' ');
add a comment |
Could try using an array of string;
var fullName: Array<string> = Array<string>();
var firstName: string = 'Bob';
var lastName: string = 'Boberanne';
fullName.push(firstName);
fullName.push(lastName);
In html:
<p>fullName.join(' ')</p>
<p>[fistname, lastName].join(' ')</p>
In typescript:
fullName.join(' ');
[firstName, lastName].join(' ');
add a comment |
Could try using an array of string;
var fullName: Array<string> = Array<string>();
var firstName: string = 'Bob';
var lastName: string = 'Boberanne';
fullName.push(firstName);
fullName.push(lastName);
In html:
<p>fullName.join(' ')</p>
<p>[fistname, lastName].join(' ')</p>
In typescript:
fullName.join(' ');
[firstName, lastName].join(' ');
Could try using an array of string;
var fullName: Array<string> = Array<string>();
var firstName: string = 'Bob';
var lastName: string = 'Boberanne';
fullName.push(firstName);
fullName.push(lastName);
In html:
<p>fullName.join(' ')</p>
<p>[fistname, lastName].join(' ')</p>
In typescript:
fullName.join(' ');
[firstName, lastName].join(' ');
answered Dec 26 '18 at 21:02
RetroCoder
1,25094272
1,25094272
add a comment |
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%2f53238881%2fcomputed-property-in-typescript%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
Have a look at the Accessors section here. I think a getter (get) might do what you need.
– R. Richards
Nov 10 '18 at 12:25