如何在服务器上使用Typescript DOM库?
我正在为谷歌云函数编写代码。这里我想使用URL标准,包括如何在服务器上使用Typescript DOM库?,typescript,google-cloud-functions,typescript-lib-dom,Typescript,Google Cloud Functions,Typescript Lib Dom,我正在为谷歌云函数编写代码。这里我想使用URL标准,包括URLSearchParams。我发现它们是typescriptdom库的一部分,所以我将其添加到tsconfiglib设置中 然而,当我编译和部署云函数时,我得到一个运行时错误,说URLSearchParams没有定义 我错过了什么?我用的是TS 2.6 这是我的配置: { "compilerOptions": { /* Basic Options */ "target": &
URLSearchParams
。我发现它们是typescriptdom库的一部分,所以我将其添加到tsconfiglib
设置中
然而,当我编译和部署云函数时,我得到一个运行时错误,说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"
}
您只需更新编译器选项以包括
dom
:
之前:
"lib": ["es6", "es7", "esnext"],
之后:
"lib": ["es6", "es7", "esnext", "dom"],
这是具有URLSearchParams
界面的库
通过上述更改,我可以在src/sub/temp.ts
中放置以下内容:
let x: URLSearchParams;
我得到了以下关于hover的类型信息,以及自动完成
interface URLSearchParams
var URLSearchParams: {
new (init?: string | URLSearchParams | undefined): URLSearchParams;
prototype: URLSearchParams;
}
没有编译器警告
这都是基于TypeScript v2.6.1。。。您没有使用旧版本,是吗(v2.2之前?)能否共享(缩写的)package.config和tsconfig.json,以便我们可以尝试复制?如果函数在开发期间存在(在.d.ts文件或tsconfig文件中),但在运行时失败,这通常意味着您的实际网站(html页面)尚未加载所需的库,或者您的浏览器不支持您使用的功能。@Kokodoko我没有使用浏览器。问题是如何在服务器上使用此APIserver@Fenton我补充说them@Kokodoko为了澄清,Node的较新版本已经实现了这个API,但是由于Google云函数使用Node 6,所以我尝试使用Typescript实现。啊,很抱歉,我发布的配置来自不同的分支。正如我在问题描述中所写,我已经将lib添加到配置中。我将更新configIt,通过该添加,我已经添加了如何测试它。您使用的是什么版本的TypeScript?