I have object of type any and I assign object of type myResponse like below
public obj: any;
public set Result() {
obj = myResponse;
}
I have another function where I want to cast any type to my specific type like below
public myFunction(){
let x: MyResponse = (MyResponse) obj;
conosle.log(x.somePropoerty);
}
I have tried different methods which I got online like angular brackets for casting, Object.assign but it did not work.
MyResponse class is like below
export class MyResponse{
public property1: string;
public property2: number;
//some other code
}
I have object of type any and I assign object of type myResponse like below
public obj: any;
public set Result() {
obj = myResponse;
}
I have another function where I want to cast any type to my specific type like below
public myFunction(){
let x: MyResponse = (MyResponse) obj;
conosle.log(x.somePropoerty);
}
I have tried different methods which I got online like angular brackets for casting, Object.assign but it did not work.
MyResponse class is like below
export class MyResponse{
public property1: string;
public property2: number;
//some other code
}
Share
Improve this question
edited Jul 22, 2017 at 9:35
Maciej Treder
12.4k5 gold badges56 silver badges77 bronze badges
asked Jul 22, 2017 at 7:32
ViruViru
2,2462 gold badges19 silver badges29 bronze badges
6
-
What do you mean? Is
MyResponse
a class or an interface? How it is defined? Does the content ofobj
at least match the same interface? Explain. – Neil Lunn Commented Jul 22, 2017 at 7:46 - MyResponse is class...class has some property..Yes content obj should match the same class...As I assigned instance of same class to obj and now I am trying to convert it back to MyResponse class. – Viru Commented Jul 22, 2017 at 7:51
-
So how do you usually get a class instance from some provided data? Pretty sure you do that on a regular basis, and then why would you think this would be any different. So think! What does
new Date(0)
actually do? – Neil Lunn Commented Jul 22, 2017 at 7:53 -
obj = myResponse;
should bethis.obj = myResponse as MyResponse;
– Hitmands Commented Jul 22, 2017 at 8:15 - Have you tried this let x: MyResponse = Object.assign({},myResponse ) and x['someProperty'] to get the value – Sreemat Commented Jul 22, 2017 at 8:58
3 Answers
Reset to default 2There is no casting in TypeScript, only type assertions. You can assert that obj
is of type MyResponse
by doing:
let x: MyResponse = obj as MyResponse;
Do note that this is for pile-time checking only. If your obj
is not a MyResponse
instance at runtime then this won't work.
Casting (Microsoft call it Type assertions) in TypeScript can be done in two ways.
Let's say we got following object definition:
class MyClass {
public someMethod:void() {
console.log("method invoked");
}
public static staticMethod(object: MyClass) {
console.log("static method");
object.someMethod();
}
}
The first way of casting is very similar to Java style. You need to use <>
signs.
let someVariable: any = new MyClass();
someVariable.someMethod(); //won't pile
MyClass.staticMethod(someVariable); //won't pile
(<MyClass> someVariable).someMethod(); //will pile
MyClass.staticMethod(<MyClass> someVariable); //will pile
Second way is as @Saravana showed (using as
keyword):
//all below lines piles
let someVariable: any = new MyClass();
let another: MyClass = someVariable as MyClass;
(someVariable as MyClass).someMethod();
MyClass.staticMethod(someVariable as MyClass);
Check out this link for more informations: https://www.typescriptlang/docs/handbook/basic-types.html#type-assertions
To properly cast into "Type", you basically need to change the prototype of the object. That can be done by creating a new instance using the Constructor function, then copying your existing properties.
Otherwise type assertion will only provide a pile-time check! During runtime the "someMethod" will be undefined
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745464807a4628869.html
评论列表(0条)