简体   繁体   English

打字稿-将环境声明应用于未知对象

[英]Typescript- Apply ambient declaration to an unknown object

I have a external library "gapi" set to a property on the window object: window.gapi.我有一个外部库“gapi”设置为窗口对象的一个​​属性:window.gapi。 I'd like to keep it there but still use the @types/gapi declaration, something like:我想保留它,但仍然使用 @types/gapi 声明,例如:

import gapi from "@types/gapi"
const gapi = window.gapi as gapi

Can't seem to import it directly from node_modules either because it's ambient, ex:似乎也无法直接从 node_modules 导入它,因为它是环境的,例如:

import gapi from "./node_modules/@types/gapi/index.d.ts"

Very stuck on this if anyone understands how it works.如果有人了解它是如何工作的,那么就会非常坚持这一点。 Thanks!谢谢!

You need to make ensure you have added the types in your tsconfig file, not in your code.您需要确保已在 tsconfig 文件中添加类型,而不是在代码中。

First npm install @types/gapi && npm install @types/gapi.auth2首先npm install @types/gapi && npm install @types/gapi.auth2

Then the tsconfig.json file must contain { "compilerOptions": { "types": ["gapi", "gapi.auth2"] }}那么tsconfig.json文件必须包含{ "compilerOptions": { "types": ["gapi", "gapi.auth2"] }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM