Skip to content

Commit

Permalink
PackageJson: Fix Imports and Exports types (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Feb 17, 2023
1 parent d393c50 commit 3ebab0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions source/package-json.d.ts
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};

/**
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
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

0 comments on commit 3ebab0d

Please sign in to comment.