TypeError: “x.write” 不是一个函数

huangapple go评论93阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年2月27日 17:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定