Import statements for "indirectly referenced" type definitions in emitted declaration files seem to be inconsistent or even broken under certain circumstances.
What do I intend for "indirectly referenced"?
import type {IMyInterface} from './some/file.ts';
function myFunction(): IMyInterface {
return {};
}
export {myFunction};IMyInterface is "indirectly referenced" wherever myFunction is consumed.
There are four examples in this monorepo, each one represents a different approach to achieve the same goal, but different results are obtained as follows.
Each example consists in a package exporting a typed function and a package importing it. The issue is to be searched in declaration files generated for the importing package.
yarnyarn run build
defines/types.ts
interface IEntity {
propA: string
propB: number
propC: boolean
}
export type {IEntity};utils/create-entity.ts
import type {IEntity} from "../../defines/types";
function createEntity(): IEntity {
return {
propA: '',
propB: 0,
propC: false
}
}
export {createEntity};index.ts
import {createEntity} from "./utils/create-entity";
import type {IEntity} from "./defines/types";
export {createEntity};
export type {IEntity};index.ts
import {createEntity} from '@scope/package-a-export';
const entity = createEntity();
export {entity};types/index.d.ts
declare const entity: import("../../package-a-export/types/defines/types").IEntity;
export { entity };
//# sourceMappingURL=index.d.ts.mapThe following statement import("../../package-a-export/types/defines/types") is broken. Will only work locally, but not
if the package is installed elsewhere.
index.ts
interface IEntity {
propA: string
propB: number
propC: boolean
}
function createEntity(): IEntity {
return {
propA: '',
propB: 0,
propC: false
}
}
export {createEntity};
export type {IEntity};index.ts
import {createEntity} from '@scope/package-b-export';
const entity = createEntity();
export {entity};types/index.d.ts
declare const entity: import("@scope/package-b-export").IEntity;
export { entity };
//# sourceMappingURL=index.d.ts.mapThe following statement import("@scope/package-b-export") works, even if a static import would be preferred.
defines/types.ts
interface IEntity {
propA: string
propB: number
propC: boolean
}
export type {IEntity};utils/create-entity.ts
import type {IEntity} from "../../defines/types";
function createEntity(): IEntity {
return {
propA: '',
propB: 0,
propC: false
}
}
export {createEntity};index.ts
import {createEntity} from "./utils/create-entity";
import type {IEntity} from "./defines/types";
export {createEntity};
export type {IEntity};index.ts
import {createEntity} from '@scope/package-c-export';
import type {IEntity} from '@scope/package-c-export';
const entity = createEntity();
export {entity};
export type {IEntity};types/index.d.ts
import type { IEntity } from '@scope/package-c-export';
declare const entity: IEntity;
export { entity };
export type { IEntity };
//# sourceMappingURL=index.d.ts.mapThe following statement import type { IEntity } from '@scope/package-c-export'; is ideal, but
the mean to achieve it (at index.ts) is not.
defines/types.ts
interface IEntity {
propA: string
propB: number
propC: boolean
}
export type {IEntity};utils/create-entity.ts
import type {IEntity} from "../defines/types";
function createEntity(): IEntity {
return {
propA: '',
propB: 0,
propC: false
}
}
export {createEntity};
export type {IEntity};index.ts
import {createEntity} from "./utils/create-entity";
import type {IEntity} from "./defines/types";
export {createEntity};
export type {IEntity};index.ts
import {createEntity} from '@scope/package-d-export';
const entity = createEntity();
export {entity};types/index.d.ts
declare const entity: import("../../package-d-export/types/defines/types").IEntity;
export { entity };
//# sourceMappingURL=index.d.ts.mapThe following statement import("../../package-d-export/types/defines/types") is broken. Will only work locally, but not
if the package is installed elsewhere.