英文:
TypeError: "x.write" is not a function
问题
PanelRenderer.rendererBody = function (oR, oItem) {
const sText = oItem.getText();
if (sText) {
oR.write("<h1>");
oR.write("</h1>");
oR.writeBack(sText);
}
};
PanelRenderer.render = function (oR, oItem) {
this.start(oR, oItem);
this.rendererBody(oR, oItem);
this.end(oR);
};
英文:
PanelRenderer.rendererBody = function (oR, oItem) {
const sText= oItem.getText();
if (sText) {
oR.write("<h1>");
oR.write("</h1>");
oR.writeBack(sText);
}
};
PanelRenderer.render = function (oR, oItem) {
this.start(oR, oItem);
this.rendererBody(oR, oItem);
this.end(oR);
};
I cannot reproduce this error so I would like to ask any possible reasons of this type error
This error is in the compiled version.
What I thought was undefined, so put ?. in front of write function like oR?.write( )
But not sure cause i cannot reproduce it.
And how can I fix if it cannot be reproduced?
答案1
得分: 0
如果你在代码中随机看到这条消息,那么在你的代码中,至少有一处将某些东西作为第一个参数(即oR
)传递给了你的render
函数(因此也传递给了rendererBody
函数),而这个东西的类型不正确,因此没有write
函数。
TypeScript 只能确保编译时的类型安全。也就是说,当运行时的数据与期望的数据不符时,TypeScript 无法解决这个问题。
修复可能会很困难,如果问题不能(可靠地)复现。你只能检查调用render
(或rendererBody
)的地方,查看传递给该函数的数据来自何处,以及是否有可能不符合你的预期。
英文:
If you see that message randomly, at (at least) one point in your code something is passed as first parameter (ie as oR
) to your render
(and therefore also rendererBody
) function, that doesn't have the correct type, and thus does not have a write
function.
Typescript can only assure compile time type safety. Ie, when the data at runtime differs from the expected data, there is nothing typescript can do about it.
Fixing might be hard, if it's not (reliably) reproducible. You can only check where your render
(or rendererBody
) is called, and see where the data you pass into that function comes from and if it by any chance might not be, what you expect it to be.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论