【问题标题】:How to use Typescript DOM lib on the server?如何在服务器上使用 Typescript DOM lib?
【发布时间】:2018-05-11 18:35:56
【问题描述】:

我正在为 Google 云功能编写代码。这里我想使用包括URLSearchParams 在内的URL 标准。我发现它们是 TypeScript DOM 库的一部分,所以我已将其添加到我的 tsconfig lib 设置中。

但是,当我编译和部署云函数时,我收到一个运行时错误,提示 URLSearchParams 未定义。

我错过了什么?我正在使用 TS 2.6


这是我的配置:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6",
    "module": "commonjs",
    "lib": ["es6", "es7", "esnext", "dom"],

    "sourceMap": true /* Generates corresponding '.map' file. */,
    "outDir": "build" /* Redirect output structure to the directory. */,
    "removeComments": true /* Do not emit comments to output. */,

    /* Strict Type-Checking Options */
    "strict": true /* Enable all strict type-checking options. */,

    /* Additional Checks */
    "noUnusedLocals": true /* Report errors on unused locals. */,
    "noUnusedParameters": true /* Report errors on unused parameters. */,
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
    "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,

    "plugins": [{ "name": "tslint-language-service" }],
    "skipLibCheck": false
  },
  "include": ["src/**/*"],
  "exclude": ["build"]
}

以及 package.json 依赖:

  "dependencies": {
    "circular-json": "^0.4.0",
    "es6-promisify": "^5.0.0",
    "firebase-admin": "^5.5.0",
    "firebase-functions": "^0.7.3",
    "invariant": "^2.2.2",
    "joi": "12",
    "lodash": "^4.17.4",
    "node-fetch": "^2.0.0-alpha.9"
  },
  "devDependencies": {
    "@types/circular-json": "^0.4.0",
    "@types/invariant": "^2.2.29",
    "@types/joi": "^13.0.0",
    "@types/lodash": "^4.14.85",
    "@types/node": "^8.0.52",
    "@types/node-fetch": "^1.6.7",
    "cpy-cli": "^1.0.1",
    "del-cli": "^1.1.0",
    "firebase-tools": "^3.15.1",
    "tslint": "^5.8.0",
    "tslint-config-prettier": "^1.6.0",
    "tslint-language-service": "^0.9.6",
    "typescript": "2.6"
  }

【问题讨论】:

  • 您能分享您的(缩写)package.config 和 tsconfig.json 以便我们尝试重现吗?
  • 如果一个函数在开发期间存在(它在你的 .d.ts 文件或 tsconfig 文件中)但在运行时失败,这通常意味着你的实际网站(html 页面)没有加载了所需的库或您的浏览器不支持您使用的功能。
  • @Kokodoko 我没有使用浏览器。问题是如何在服务器上使用此 API
  • @Fenton 我已经添加了它们
  • @Kokodoko 澄清一下,较新版本的 Node 已经实现了这个 API,但是由于 Google Cloud Functions 使用 Node 6,我正在尝试使用 Typescript 实现。

标签: typescript google-cloud-functions typescript-lib-dom


【解决方案1】:

您只需更新编译器选项以包含dom

之前:

"lib": ["es6", "es7", "esnext"],

之后:

"lib": ["es6", "es7", "esnext", "dom"],

这是带有URLSearchParams 接口的库。

仅通过上述更改,我可以将以下内容放入src/sub/temp.ts

let x: URLSearchParams;

我在悬停时获得以下类型信息,以及自动完成。

interface URLSearchParams
var URLSearchParams: {
    new (init?: string | URLSearchParams | undefined): URLSearchParams;
    prototype: URLSearchParams;
}

编译器警告为零。

这一切都基于 TypeScript v2.6.1...您使用的不是旧版本吗(v2.2 之前的版本?)。

【讨论】:

  • 抱歉,我发布的配置来自不同的分支。正如我在问题描述中所写,我已经将 lib 添加到我的配置中。我会更新配置
  • 它对我有用,我已经添加了我如何测试它。你使用的是什么版本的 TypeScript?
猜你喜欢
  • 2020-10-12
  • 2019-04-07
  • 1970-01-01
  • 2020-08-05
  • 2020-03-11
  • 2018-03-23
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
相关资源
最近更新 更多