If a module (e.g. moment.js, knockout, or big.js) is included with a <script> tag e.g.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js">
</script>
that define a global property (e.g. moment, ko, Big, etc), how can one access/declare the types on window (or global) in Typescript.
For example
const x = moment()
const t = ko.observable()
const b = new Big()
How can one set the ambient type of these globals without including the entire moment.js library? The objective is to have properly typed global references to be used by VS Code, and tsc, ts-loader, or babel-typescript.
In the case of moment, the types are exposed at node_modules/moment/moment.d.ts, but for other libraries (e.g. knockout or big.js) they're at @types/[module]/index.d.ts.
It feels like this would be quite common, but I haven't seen a good, working reference of how to accomplish this.
Here is the tsconfig:
{
"compilerOptions": {
"target": "ESNext",
"moduleResolution": "node",
"allowJs": true,
"noEmit": true,
"strict": false,
"isolatedModules": false,
"esModuleInterop": true,
"noResolve": false,
"baseUrl": ".",
"paths": {
"*": [
"*",
"js.packages/*"
]
},
"jsx": "preserve",
"outDir": "dist/"
},
"include": [
"js.packages/**/*.ts",
"js.packages/**/*.tsx",
"js.packages/@types/lib.d.ts",
],
"files": [
"services/app/src/entry.js"
],
"exclude": [
"node_modules"
]
}
Here's a lib.d.ts
declare global {
type Bigable = Big | string | number
interface Window {
Big: typeof import('big.js'),
moment: typeof import('moment'),
Sentry: typeof import('@sentry/browser'),
}
}
and here's how consumption should work:
const v = new Big(1)
const m = moment()
const s = global.Sentry()
const o = ko.observable()
which looks like this in VS Code (with the red underline indicating failures):
So knockout works because @types/knockout/index.d.ts has:
declare var ko: KnockoutStatic;
declare module "knockout" {
export = ko;
}
whereas I've similarly declared a global Big on interface Window.
Unfortunately Sentry and moment (in this example) do not seem to work, and it's unclear why or what one might have to do to fix this.

node_modules/[package]andnode_modules/@types/[package]you shouldn't need to do anything in particular. Please post yourtsconfig.json. It's perfectly possible to install the moment package just to get the types while still loading the implementation from a script tag at runtime.