I am currently working on an Electron app using TypeScript and Angular2, for which I have created many different classes, all in separate files. In order to make the refactoring of my app easier, I would like to not have to use the relative path when importing those classes.
Right now, to import them, I use this syntax: import {Trigger} from './../../trigger/ts/trigger.component';
What I would like to do is use a syntax that looks like this: import {Trigger} from 'trigger.component';
Is that possible?
To fix this, all you need is to set a compilerOptions. baseUrl config in your project's tsconfig. json file. So, if we want to make the src folder (which sits at the root of the project) as the base of every import, we can set it like so.
A relative import is one that starts with / , ./ or ../ .
The option “Do not import exactly from” allows you to tell the IDE that you don't want any imports that are from the specific path (e.g. @material-ui/core ) or path pattern (e.g. @material-ui/core/** ). The IDE will then use an alternative path if there is one.
When set to true, allowSyntheticDefaultImports allows you to write an import like: ts. import React from "react"; instead of: ts.
Better to use below configuration in tsconfig.json
{
"compilerOptions": {
"...": "reduced for brevity",
"baseUrl": "src",
"paths": {
"@app/*": ["app/*"],
"@trigger/*": ["app/trigger/ts/*"]
}
}
}
For your case, use can do either import {Trigger} from '@app/trigger/ts/trigger.component';
or import {Trigger} from '@trigger/trigger.component';
or any level of path you can configure.
You can create one file, for example: components.ts and in that component reference to all components, like:
export {Trigger} from '../trigger/ts/trigger.component'
export {Cmp} from '../cmp/ts/cmp.component'
...
Than, when you want to load one component, you can do:
import {Trigger} from '../components.ts'
It is good practice for grouping things.
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