1

I'm using a JS module, with typings from Definitely Typed, that has this structure:

declare namespace Foo {
  interface Bar {}
}

declare module "abc" {
    function f(): Foo.Bar

    namespace f {
    }

    export = f;
}

How can I import the Bar interface from "abc"?

2
  • Unfortunate you can't. The typings is not defined correctly to expose that. Commented Jul 13, 2018 at 15:46
  • You can't import what's not exported. The intent of that typing probably is to expose Foo as global object, along with "abc" module, and the only thing you can import from "abc" is f. Commented Jul 13, 2018 at 15:47

1 Answer 1

1

When something isn't explicitly exported, chances are, the author of the typings intends for you to not use the type.

...That being said, there technically is a way to get to it. TypeScript added a ReturnType type in 2.9 along with conditional types, which allows you to get the return type of a function. Here, you'd use it like this:

import f from 'abc'

type Bar = ReturnType<typeof f>

Playground

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I needed. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.