Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular dependency when importing types from other features #40

Closed
SevenOutman opened this issue Oct 9, 2021 Discussed in #39 · 1 comment
Closed

Circular dependency when importing types from other features #40

SevenOutman opened this issue Oct 9, 2021 Discussed in #39 · 1 comment

Comments

@SevenOutman
Copy link
Contributor

Discussed in #39

Originally posted by SevenOutman October 9, 2021
It's a common scenario when we import type definitions from other features, especially definitions for resources (fetched from APIs). If we expose everything from only the index.ts to other features, it's easy to encounter circular dependency.

For example, if I'm writing a components in teams feature where type User from users feature is used:

// teams/components/MyComponent.ts
import type { User } from '@/features/users';

That will cause components from users to also be loaded because I re-exported them from users/index.ts.

// users/index.ts
export * from './types';
export * from './components';

Meanwhile I have a component in users feature that imports type Team from teams feature:

// users/component/MyComponent.ts
import type { Team } from '@/features/teams';

Now my teams/components/MyComponent.ts and users/component/MyComponent.ts forms a circular dependency.

My solution is to import types from other features directly from their ./types.ts, which means that ./index.ts is not reguarded as the only port of a feature.

Same problem exists with importing APIs from other features. Any idea?

@alan2207
Copy link
Owner

alan2207 commented Oct 9, 2021

Well, if my understanding of ES modules is correct, I don't think that would be the case.

If you import a type like this:

import type { Team } from '@/features/teams';

You will import just what you specified, not everything that module is exporting. You can potentially have a circular dependency but that could happen unrelated to the ./index.ts entry point of a feature.

Can you provide a codesandbox where you can reproduce the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants