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?
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.
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.
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.
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.
In config/index.ts
re-export config as such:
export {default as config} from './config';
Then in build/build.ts
:
import {config} from '../config;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With