With the introduction of Typescript 3.8, a new syntax is available:
import type { ExternalFoo } from "foo";
If you are simply using the library for type information, there may no longer be a need to list it as a dependency
or an optionalDependency
. Instead, you could consider specifying it as a peerDependency
, ensuring that users have compatible versions with your library. Including it as a devDependency
can also be beneficial.
It's important to note that this import will only appear in the generated d.ts
files and not in the transpiled .js
code. However, if the library is not installed by users, the type will default to any
, potentially affecting your own typing. This could lead to issues such as:
customPackage = (source: any | ExternalBar) =>
// equivalent to customPackage = (source: any) =>
In scenarios where the library is missing, the type annotation won't properly utilize related types even if they are present. While there is a method to reduce dependency on external libraries, challenges still exist in maintaining type annotations that remain robust regardless of their presence.
To explore solutions for handling missing types, refer to this answer.
For more details on Type-Only Imports and Exports in Typescript 3.8, visit the reference page.