Context
I am developing an npm package using Typescript so types are being shipped alongside the library, basically:
my-package
|- index.js
|- index.d.ts
|- package.json
index.d.ts file contains globals, something like:
declare var thisIsAGlobal: string
The problem
After publishing the package and installed in another project using npm i my-package the globals are not being seen by typescript, unless you explicitly import 'my-package' or /// <reference types="my=package" /> in any file in the project, after that the globals are visible.
Project:
- node_modules
|- my-library
|- index.d.ts
- src
|- index.ts // thisIsAGlobal global not visible
|- other_file.ts // thisIsAGlobal global not visible
Discoveries
While trying to reverse engineer Jest types, that mostly export globals, I discovered that the only difference between my globals and Jest globals are the location, Jest globals are in node_modules/@types/jest/index.d.ts while mine are outside node_modules/@types, at the beginning I though there was something to do with package.json or some type configuration but I did the following experiment:
I manually created a single file (with a global in it) inside a folder inside node_modules/@types and the global was visible within my project files.
- node_modules
|- @types
|- experiment
|- index.d.ts // declare var thisIsAGlobal: number
If I take the experiment file outside the @types directory the global stop being visible within the project files.
- node_modules
|- @types
|- experiment
|- index.d.ts // declare var thisIsAGlobal: number
You don't even need a package.json file in the @types directory in order for typescript to obtain global types.
Question
Is there something I am missing while publishing a package with global types?
Maybe for types outside @types you need a special configuration?
npm i @types/jesttypescript now can see jest globals likedescribeoritand you can now use them without typescript complaining they do not exists and without you explicitly importing them likeimport { describe } from 'jest'.