lmonninger
lmonninger

Reputation: 961

How to import type from "export module" block?

I have the following buried in a Node package:

export module values {

  ...

  export type Document<T = object> = {
    ref: Ref
    ts: number
    data: T
  }

  ...

}

I need to import Document. I have tried:

import {values} from "package";

const { Document } = values;

// AND
import {Document} from "package/src/values";

How can I import a type from a export module block?

Upvotes: 0

Views: 233

Answers (1)

Connor Low
Connor Low

Reputation: 7186

Document is a type, so const { Document } = values; could never work (const is used to declare variables at runtime, whereas types are only used by TypeScript at compile-time, two different beasts). Additionally, importing from "package/src/values" would only work if values was a real file.

You were on the right track with import {values} from "package";, but you cannot destruct a module to grab a type. Instead, do one of the following:

import { values } from "package";

// Option 1: fully qualify Document when using that type
const foo: values.Document = { ... };

// Option 2: create a type alias for Document
type Document = values.Document;
const bar: Document = { ... };

Upvotes: 1

Related Questions