As detailed in this answer, Typescript 3.8 brought about:
import type
to safely import definitions (source):
import type only imports declarations for type annotations and declarations. It's completely erased, leaving no trace at runtime. Equally, export type offers an export for type contexts and is also removed from TypeScript's output.
Nevertheless, you still need to have the package as a dependency.
This can result in circular dependencies, as illustrated in my scenario. A simplified breakdown of my monorepo
involves:
client-web
: a web client usingvite
(client)client-store
: a redux store package (model)image-gallery
: an image gallery package (presentation)
All of them require awareness of the following type:
type IImage = {
id: string;
title: string;
url: string;
urlThumb: string;
}
However, the location where this type should "live" isn't clearly defined. There are a few potential options:
- place it in the presentation =>
image-gallery
and import it into the other packages - put it in the model =>
client-store
and import it into the other packages - establish a shared
common-types
package (manually) - generate an automatically shared
common-types
package (composition)
No matter which route you take, there may be challenges like complicating your dependency graph and transitioning from parallel to sequential build processes. Additionally, not all types are the same - sometimes you want types near a component, while other times you prefer grouping types by semantic context.
I'm curious if there's a solution that hasn't crossed my mind?