typescript - IDE 承认变量存在,但 Typescript 给出编译器错误
问题描述
Visual Studio 代码为我的 typescript 代码提供代码完成,并且不会给出警告/错误,但是当从命令行使用 编译它时tsc fullscreen.ts
,TypeScript 会给出错误消息
我的代码:fullscreen.ts
function checkFullscreen() {
if (document.fullscreenElement !== null) {
console.log("There is a fullscreen element");
}
else {
console.log("There is no fullscreen element");
}
}
编译器错误:
fullscreen.ts:2:18 - error TS2339: Property 'fullscreenElement' does not exist on type 'Document'.
这是我在/usr/share/code-oss/resources/app/extensions/node_modules/typescript/lib/lib.dom.d.ts
使用 VS Code 的 go to definition时发现的
interface Document extends Node, GlobalEventHandlers, ParentNode, DocumentEvent {
...
readonly fullscreenElement: Element | null;
...
}
其中省略号代表不相关的代码行。显然存在 TypeScript 接口来检查我的代码,但由于某种原因,TypeScript 编译器找不到它们。
对于上下文,这是我正在尝试使用的 html
fullscreen.html
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<button id="fullscreen" onClick="checkFullscreen()">Check Fullscreen</button>
<canvas id="canvas"></canvas>
<script src="fullscreen.js"></script>
</body>
</html>
这是我的目录中仅有的两个文件——没有 tsconfig、node_modules 等。为了以防万一它有用,这是我的打字稿版本
tsc --version
Version 3.3.3
etid:此 api 的文档
解决方案
推荐阅读
- rust - 在 Tokio 中向非线程安全的单例对象分派请求
- python - 使用加法器电路将两个 4 位二进制数相加
- flask - 如何将计数器连接到 Jinja2 表达式?
- quickfixj - 重置后是否需要重新订阅市场数据?
- node.js - axios 上传到 node.js API 导致 aws s3 中的文件变得不可读
- sql - 在databricks中使用sql创建最大日期函数
- javascript - 如何让 JavaScript 函数应用的类一直运行到窗口关闭(网页设计)
- python - 在有和没有包依赖的情况下进行测试
- javascript - 带有 Javascript 的 html 中的表格过滤器不起作用
- django - 从 Redis 丢失计划的 Celery 任务