Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export 'default' using index.ts

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?

like image 527
maelgrove Avatar asked May 29 '17 11:05

maelgrove


People also ask

Should you use index TS files?

Generally you should create index. ts file in feature specific folders. But it is up to you how many index. ts file should be created, should it be feature specific, component specific etc.

What is the use of index TS file in angular?

index. ts help us to keep all related thing together and we don't need to worry about the source file name. We can import all thing by using source folder name.

What is the use of index TS?

ts allows you to organize your imports. This file can be used to import multiple modules from other folders and re-export them so that they can be consumed by the other modules more easily. The modules that were re-exported in Index. ts can be then imported from Index.

How do I export a TS function?

YES, TypeScript can export a function! Here is a direct quote from the TS Documentation: "Any declaration (such as a variable, function, class, type alias, or interface) can be exported by adding the export keyword." Show activity on this post.


1 Answers

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

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

Then in build/build.ts:

import {config} from '../config;
like image 101
Kirill Dmitrenko Avatar answered Oct 09 '22 12:10

Kirill Dmitrenko