[英]Typescript object casting not working
I got an HTTP API. 我有一个HTTP API。 I am loading a set of objects as a json.
我正在加载一组对象作为json。
I want to cast to a Typescript object, but with the keyword "as" it is not working and even not with < Type > in front of the object. 我想要转换为Typescript对象,但是使用关键字“as”它不起作用,甚至不能在对象前面使用<Type>。
r.forEach(entry => {
entry.creationDate = new Date(entry.creationDate.date);
entry.creator = <User>entry.creator;
return entry;
});
The console.log directly after entry.creator casting outputs a normal "Object". 在entry.creator转换后直接输出console.log输出一个普通的“对象”。
Can someone give me an advice? 有人可以给我一个建议吗?
I I've struggled with similar problems, and in my opinion this is a defect in typescript. 我一直在努力解决类似的问题,在我看来这是打字稿中的缺陷。 When you are doing the cast as you did.
当你像你一样做演员表时。 Or a sample code like this:
或者像这样的示例代码:
class User {
name: string;
doConsole(): void {
console.log(`Name: ${this.name}`);
}
}
let userObj = { name: 'jose' };
let user = new User();
Object.assign(user, userObj);
user.doConsole();
You will notice that doConsole won't be a function in the casted object. 您会注意到doConsole将不是已转换对象中的函数。 That's the generated JS to this:
这就是生成的JS:
var User = (function () {
function User(name) {
this.name = name;
}
User.prototype.doConsole = function () {
console.log("Name: " + this.name);
};
return User;
}());
var userObj = { name: 'jose' };
var user = userObj;
user.doConsole();
As you can see it doesn't uses the prototype function you prepared by the class when doing the cast. 正如您所看到的那样,它不会使用您在进行强制转换时由类准备的原型函数。 My alternative was to do something like this:
我的另一种选择是做这样的事情:
class User {
name: string;
doConsole(): void {
console.log(`Name: ${this.name}`);
}
}
let userObj = { name: 'jose' };
let user = new User();
Object.assign(user, userObj);
user.doConsole();
This ensures that you are using the prototype function, as you can see by the generated JS: 这可以确保您使用原型函数,正如生成的JS所示:
var User = (function () {
function User() {
}
User.prototype.doConsole = function () {
console.log("Name: " + this.name);
};
return User;
}());
var userObj = { name: 'jose' };
var user = new User();
Object.assign(user, userObj);
user.doConsole();
So basically what I'm saying is that I agree with you that it should works like you did, but the transpiler doesn't use the prototyped function, so it won't work. 所以基本上我所说的是我同意你的意思,它应该像你一样工作,但是转换器不使用原型函数,所以它不起作用。
I hope this helps you. 我希望这可以帮助你。
Javascript itself doesn't support strong typings. Javascript本身不支持强类型。 You can use strong typing only with Typescript but it only works for compile-time.
您只能在Typescript中使用强类型,但它仅适用于编译时。 What you are experiencing on runtime is the expected behaviour.
您在运行时遇到的是预期的行为。 Not a bug or error with your code.
您的代码不是错误或错误。
Some of the great features that Typescript provides are:
Typescript提供的一些强大功能包括:
- Compile time checking: Find potential errors on your code before your customer does.
编译时间检查:在客户执行之前查找代码上的潜在错误。
- Strongly-typed objects: By specifying the type of objects your functions expect you can reduce the risk of unexpected results.
强类型对象:通过指定函数所期望的对象类型,可以降低意外结果的风险。
Source of my quote and more info about strong typing here . 我的报价渊源及强类型的详细信息在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.