Mixing code and attributes in TypeScript class
I have a simple TypeScript class defined like this:
export class Piece {
Qty: number;
Weight: number;
Type: string;
totalWeight() {
return Qty * Weight;
}
}
And, I have an Angular 5 service that has a method like this:
get(id: number) {
return this.http.get<Piece>(myAPIURL + id);
}
And my Angular code calls this as follows:
const myPiece: Piece;
this.myService.get(2).subscribe(data => myPiece = data);
When all is said and done, the variable 'myPiece' contains the correct data; however, there is no totalWeight() method attached to it. I think I understand why (because the server returned the data/attributes, but obviously no code). But is there a way to somehow unite the data and the code/methods again?
My background is with C# objects, and I'm still new to Java/TypeScript. I'm guessing this may have something to do with the prototypical inheritance in JavaScript?
Any help?
angular typescript
add a comment |
I have a simple TypeScript class defined like this:
export class Piece {
Qty: number;
Weight: number;
Type: string;
totalWeight() {
return Qty * Weight;
}
}
And, I have an Angular 5 service that has a method like this:
get(id: number) {
return this.http.get<Piece>(myAPIURL + id);
}
And my Angular code calls this as follows:
const myPiece: Piece;
this.myService.get(2).subscribe(data => myPiece = data);
When all is said and done, the variable 'myPiece' contains the correct data; however, there is no totalWeight() method attached to it. I think I understand why (because the server returned the data/attributes, but obviously no code). But is there a way to somehow unite the data and the code/methods again?
My background is with C# objects, and I'm still new to Java/TypeScript. I'm guessing this may have something to do with the prototypical inheritance in JavaScript?
Any help?
angular typescript
add a comment |
I have a simple TypeScript class defined like this:
export class Piece {
Qty: number;
Weight: number;
Type: string;
totalWeight() {
return Qty * Weight;
}
}
And, I have an Angular 5 service that has a method like this:
get(id: number) {
return this.http.get<Piece>(myAPIURL + id);
}
And my Angular code calls this as follows:
const myPiece: Piece;
this.myService.get(2).subscribe(data => myPiece = data);
When all is said and done, the variable 'myPiece' contains the correct data; however, there is no totalWeight() method attached to it. I think I understand why (because the server returned the data/attributes, but obviously no code). But is there a way to somehow unite the data and the code/methods again?
My background is with C# objects, and I'm still new to Java/TypeScript. I'm guessing this may have something to do with the prototypical inheritance in JavaScript?
Any help?
angular typescript
I have a simple TypeScript class defined like this:
export class Piece {
Qty: number;
Weight: number;
Type: string;
totalWeight() {
return Qty * Weight;
}
}
And, I have an Angular 5 service that has a method like this:
get(id: number) {
return this.http.get<Piece>(myAPIURL + id);
}
And my Angular code calls this as follows:
const myPiece: Piece;
this.myService.get(2).subscribe(data => myPiece = data);
When all is said and done, the variable 'myPiece' contains the correct data; however, there is no totalWeight() method attached to it. I think I understand why (because the server returned the data/attributes, but obviously no code). But is there a way to somehow unite the data and the code/methods again?
My background is with C# objects, and I'm still new to Java/TypeScript. I'm guessing this may have something to do with the prototypical inheritance in JavaScript?
Any help?
angular typescript
angular typescript
asked Nov 23 '18 at 21:29
Rob SchripsemaRob Schripsema
135
135
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
you need to actually instantiate the class from the http call. TypeScript is just javascript with type hinting, not actual casting can be done, because javascript doesn't have this. So to make something a certain class, you really have to instantiate this with the new
keyword
get(id: number) {
return this.http.get<Piece>(myAPIURL + id).pipe(
map((data) => ({ ...new Piece(), ...data}))
)
}
or
map((data) => Object.assign(new Piece(), data))
Whatever you find more legible
Exactly what I was looking for! I'll give it a try and see how it goes.
– Rob Schripsema
Nov 23 '18 at 22:38
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%2f53453132%2fmixing-code-and-attributes-in-typescript-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you need to actually instantiate the class from the http call. TypeScript is just javascript with type hinting, not actual casting can be done, because javascript doesn't have this. So to make something a certain class, you really have to instantiate this with the new
keyword
get(id: number) {
return this.http.get<Piece>(myAPIURL + id).pipe(
map((data) => ({ ...new Piece(), ...data}))
)
}
or
map((data) => Object.assign(new Piece(), data))
Whatever you find more legible
Exactly what I was looking for! I'll give it a try and see how it goes.
– Rob Schripsema
Nov 23 '18 at 22:38
add a comment |
you need to actually instantiate the class from the http call. TypeScript is just javascript with type hinting, not actual casting can be done, because javascript doesn't have this. So to make something a certain class, you really have to instantiate this with the new
keyword
get(id: number) {
return this.http.get<Piece>(myAPIURL + id).pipe(
map((data) => ({ ...new Piece(), ...data}))
)
}
or
map((data) => Object.assign(new Piece(), data))
Whatever you find more legible
Exactly what I was looking for! I'll give it a try and see how it goes.
– Rob Schripsema
Nov 23 '18 at 22:38
add a comment |
you need to actually instantiate the class from the http call. TypeScript is just javascript with type hinting, not actual casting can be done, because javascript doesn't have this. So to make something a certain class, you really have to instantiate this with the new
keyword
get(id: number) {
return this.http.get<Piece>(myAPIURL + id).pipe(
map((data) => ({ ...new Piece(), ...data}))
)
}
or
map((data) => Object.assign(new Piece(), data))
Whatever you find more legible
you need to actually instantiate the class from the http call. TypeScript is just javascript with type hinting, not actual casting can be done, because javascript doesn't have this. So to make something a certain class, you really have to instantiate this with the new
keyword
get(id: number) {
return this.http.get<Piece>(myAPIURL + id).pipe(
map((data) => ({ ...new Piece(), ...data}))
)
}
or
map((data) => Object.assign(new Piece(), data))
Whatever you find more legible
answered Nov 23 '18 at 21:44
PierreDucPierreDuc
30.2k56078
30.2k56078
Exactly what I was looking for! I'll give it a try and see how it goes.
– Rob Schripsema
Nov 23 '18 at 22:38
add a comment |
Exactly what I was looking for! I'll give it a try and see how it goes.
– Rob Schripsema
Nov 23 '18 at 22:38
Exactly what I was looking for! I'll give it a try and see how it goes.
– Rob Schripsema
Nov 23 '18 at 22:38
Exactly what I was looking for! I'll give it a try and see how it goes.
– Rob Schripsema
Nov 23 '18 at 22:38
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.
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%2f53453132%2fmixing-code-and-attributes-in-typescript-class%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