在 VS Code 中使用 TypeScript 在最新版本上出现 Canvas 错误 [英] Using TypeScript in VS Code giving Canvas errors on latest version

查看:42
本文介绍了在 VS Code 中使用 TypeScript 在最新版本上出现 Canvas 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 TypeScript 为我的应用编写一些 Canvas 代码,但出现以下错误:

I am writing some Canvas code in TypeScript for my app and get the following error:

Property 'wrapText' does not exist on type 'CanvasRenderingContext2D'.ts(2339)

这是来自一行:

CanvasRenderingContext2D.prototype.wrapText = function(text, x, y, maxWidth, lineHeight) 

我想我可以在 lib.dom.d.ts 文件中添加一个声明,该文件位于 VS Code 的 node_modules 扩展部分.这是修复它的唯一方法吗?我很好奇为什么不包括在内.我正在运行 2021 年 4 月版本的 VS Code,因为我刚刚升级了它.

I guess I can add a declaration in the lib.dom.d.ts file which is in the node_modules extensions part of VS Code. Is this the only way to fix it? I am curious why it wasn't included. I am running the April 2021 version of VS Code as I just upgraded it.

推荐答案

我找不到可以证明上下文中应该有一个现有的 wrapText 定义的参考,它不一定即使有这样的成员属性也是可写的.

I can't find a reference that demonstrates there SHOULD be an existing wrapText definition on the context, and it wouldn't necessarily be writable even if there was such a member property.

我认为这种猴子补丁模式正在添加一个以前根本不存在的新属性.

I think this monkey-patching pattern is adding a new property which doesn't exist at all before.

我将此解释为打字稿的正常过度属性检查行为,这意味着您不能将意外属性分配给预定义类型的实例.

I interpret this as being the normal excess property check behavior of typescript meaning you can't assign unexpected properties to instances of pre-defined types.

https://riptutorial.com/html5-canvas/example/18590/wrapping-text-into-paragraphs 声明没有预先存在的实现并采用类似的模式(在 javascript 中没有多余的属性检查,因此没有错误).

https://riptutorial.com/html5-canvas/example/18590/wrapping-text-into-paragraphs states there IS NO pre-existing implementation and adopts a similar pattern (in javascript where there are no excess property checks so no error).

在不操作任何 node_modules 或 VSCode 元数据的情况下,您仍然可以通过使用全局增强来安排使这个可选属性在您的项目本地的 CanvasRenderingContext2D 上有效... https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation

Without manipulating any node_modules or VSCode metadata you can still arrange to make this optional property valid on a CanvasRenderingContext2D locally to your project by using Global Augmentation... https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation

这篇关于在 VS Code 中使用 TypeScript 在最新版本上出现 Canvas 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆