简体   繁体   中英

Typescript- Apply ambient declaration to an unknown object

I have a external library "gapi" set to a property on the window object: window.gapi. I'd like to keep it there but still use the @types/gapi declaration, something like:

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:

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.

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

Then the tsconfig.json file must contain { "compilerOptions": { "types": ["gapi", "gapi.auth2"] }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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