15

I've got the following project structure:

build/
    build.ts
config/
    config.ts
    index.ts
...

The config.ts contains a default exported type like this:

export default {
    myProp: {
        someProp: "someValue"
    }
}

And the index.ts within config/ looks like this:

export * from './config';

Now I'd like to import the config type within build.ts like this:

import config from '../config';

But when using it (e.g. with config.myProp), it tells me that myProp doesn't exist on index.ts.

According to the official module documentation here, this should work perfectly fine. Am I missing something here?

2
  • How about import * as config from "../config";? Commented May 29, 2017 at 11:11
  • @Granga Thanks, but same result. PropertyXY does not exist on type typeof ...index. Commented May 29, 2017 at 11:20

2 Answers 2

42

In config/index.ts re-export config as such:

export {default as config} from './config';

Then in build/build.ts:

import {config} from '../config;
Sign up to request clarification or add additional context in comments.

2 Comments

I wanted to avoid explicitly named exports because the entire config/ folder is just 'one module' so there was no point in doing that. But you got me on the right track. I ended up doing import default as config from '../config' and it magically started working. So thank you.
The first line won't even compile for me. It states that "config" has no defined "default" property, even though the file does have a export default statement. Typescript v3.1.6
10

It seems like there is a trend towards not using default export, due to the number of potential issues. The recommendation is to use named exports. I am myself happily following the convention to only use named exports. The reasons as shown here matches my experience on the subject.

But, should you still choose to export as default, then I think you should be able to re-export it in config/index.ts like this:

export {default} from './config';

Then in build/build.ts you should be able to do a

import config from '../config';

Comments

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.