Querying Library Types in a .d.ts Declaration File
I am currently working on creating a custom namespace in a cf.d.ts
file to define general-purpose types for my application. This allows me to avoid importing modules repeatedly whenever I need to reference Foo
.
declare namespace CF {
export type Foo = number
}
While this approach works well for types that encapsulate primitives or other custom types within my declaration files, I now face the challenge of referencing types from an external package that my project depends on. Specifically, I need to use types like Instance
and IAnyType
from the mobx-state-tree
package:
declare namespace CF {
/**
* Represents the parameters to a query
*/
export type QueryParams<
F extends Instance<IAnyType>,
R extends Instance<IAnyType>,
> = [F[], R[], number]
}
The issue arises because TypeScript is unable to determine where these types originate from, leading them to be inferred as any
. To provide accurate typings, I have resorted to prefixing import('mobx-state-tree').
before each usage of these types:
declare namespace CF {
/**
* Represents the parameters to a query
*/
export type QueryParams<
F extends import('mobx-state-tree').Instance<import('mobx-state-tree').IAnyType>,
R extends import('mobx-state-tree').Instance<import('mobx-state-tree').IAnyType>,
> = [F[], R[], number]
}
However, I aim to streamline this process by importing these types once within my declaration and then referencing them throughout. Unfortunately, directly importing with
import { ... } from 'mobx-state-tree'
limits visibility of the QueryParams
type to modules utilizing the CF
namespace.
Various approaches I have experimented with include:
import type { ... } from 'mobx-state-tree'
/// <reference types="mobx-state-tree" />
(and subsequently using MST.Instance<...>)import MST = require('mobx-state-tree')