I am currently working on a private project using TypeScript and Yarn. In this project, I have developed a package that is meant to be utilized by one or more other applications.
However, as I started to work on the consumer application, I encountered an issue:
One of my const object exports is recognized as a type but cannot be resolved to its value. This causes compilation errors in the consumer code when trying to access the value. Interestingly, const string literals work just fine within the package itself.
Here are some additional details:
- TypeScript version: ^5.3.3
- Tsup version: ^8.0.2 for package compilation
- Using yarn link to provide the package locally to the consumer
- The object and the type share the same name. My research indicates that this is acceptable since types and values are in different spaces.
Minimal (non-)working example:
Package:
/* ./types/constants.ts */
export const On = "on";
export const Off = "off";
export const PlayState = {
Play: "play",
Pause: "pause",
Stop: "stop",
} as const;
export type OnOff = typeof On | typeof Off;
export type PlayState = (typeof PlayState)[keyof typeof PlayState];
/* ./index.ts */
import * as Constants from "./types/constants.js";
const a: Constants.PlayState = Constants.PlayState.Play; // compiles as expected
export * as Constants from "./types/constants.js";
Consumer:
/* ./index.ts */
import { Constants } from "@author/pkg";
const a: Constants.OnOff = Constants.On; // works fine
const b: Constants.PlayState = "play"; // works fine
// const c: Constants.PlayState = Constants.PlayState.Play;
// displays error: "Property 'PlayState' does not exist on type 'typeof constants'"
I initially expected the imported package to function similarly to how it does internally, based on online information suggesting that it should work. The compiled code appears correct to me, with the PlayState export and declaration included, but I may be overlooking something.
Any assistance would be greatly appreciated!