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

fix(PackageJson): ensure Imports and Exports reflect Node.js expectations #555

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions source/package-json.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ declare namespace PackageJson {
string
>;

type ExportConditions = {[condition in ExportCondition]: Exports};
/**
A mapping of conditions and the paths to which they resolve.
*/
type ExportConditions = {[condition in ExportCondition]?: Exports};
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ? here introduces undefined as a valid exports value in the type, which is not possible by JSON.


/**
Entry points of a module, optionally with conditions and subpath exports.
Expand All @@ -233,14 +236,13 @@ declare namespace PackageJson {
| null
| string
| Array<string | ExportConditions>
| ExportConditions
| {[path: string]: Exports};
| ExportConditions;

/**
Import map entries of a module, optionally with conditions.
Import map entries of a module, optionally with conditions and subpath imports.
*/
export type Imports = { // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
[key: `#${string}`]: string | {[key in ExportCondition]?: Exports};
[key: `#${string}`]: Exports;
};

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
Expand Down
10 changes: 10 additions & 0 deletions test-d/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,19 @@ expectAssignable<PackageJson.Imports>({'#unicorn': 'unicorn'});
expectAssignable<PackageJson.Imports>({
'#unicorn': {
import: {browser: 'unicorn', node: 'pony'},
require: ['./fallback-1', './fallback-2', {default: './fallback-3', browser: null}],
custom: null,
default: 'horse',
},
});
expectAssignable<PackageJson.Exports>({
'./unicorn': {
import: {browser: './unicorn.js', node: './pony.js'},
require: ['./fallback-1', './fallback-2', {default: './fallback-3', browser: null}],
custom: null,
default: './horse.js',
},
});
expectNotAssignable<PackageJson.Imports>({unicorn: 'unicorn'});
expectType<boolean | undefined>(packageJson.preferGlobal);
expectType<boolean | undefined>(packageJson.private);
Expand Down