Vlady Veselinov
Vlady Veselinov

Reputation: 5431

How to re-export types as global in TypeScript

I would like to have an interface (defined in a separate file) be accessible globally, how do I do this?

Here's my globals.d.ts file

import { Theme, Style } from './style/themes/theme.types'

declare global {
  Theme
  Style
}

This gives me an error

Statements are not allowed in ambient contexts.ts(1036)
'Theme' only refers to a type, but is being used as a value here.ts(2693)

Upvotes: 4

Views: 2122

Answers (1)

sparebytes
sparebytes

Reputation: 13136

This might be a hack as this raises an error if placed inside a .ts file but seems to work when placed inside a .d.ts file.

The idea is to import the type with one name and export with another.

src/globals.d.ts:

import type { DeepPick as _DeepPick } from "ts-deep-pick";
declare global {
  export { _DeepPick as DeepPick };
}

Upvotes: 3

Related Questions