Skip to content

Commit

Permalink
Fix deep properties of PackageJson and TsConfigJson (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed May 24, 2022
1 parent 638d597 commit f2aae51
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
14 changes: 7 additions & 7 deletions source/package-json.d.ts
Expand Up @@ -200,12 +200,12 @@ declare namespace PackageJson {
Run with the `npm restart` command, after `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided.
*/
postrestart?: string;
} & Record<string, string>;
} & Partial<Record<string, string>>;

/**
Dependencies of the package. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or Git URL.
*/
export type Dependency = Record<string, string>;
export type Dependency = Partial<Record<string, string>>;

/**
Conditions which provide a way to resolve a package entry point based on the environment.
Expand Down Expand Up @@ -262,7 +262,7 @@ declare namespace PackageJson {
*/
browser?:
| string
| Record<string, string | false>;
| Partial<Record<string, string | false>>;

/**
Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.
Expand All @@ -281,7 +281,7 @@ declare namespace PackageJson {
/**
Version selection map of TypeScript.
*/
typesVersions?: Record<string, Record<string, string[]>>;
typesVersions?: Partial<Record<string, Partial<Record<string, string[]>>>>;

/**
Location of the bundled TypeScript declaration file. Alias of `types`.
Expand Down Expand Up @@ -442,7 +442,7 @@ declare namespace PackageJson {
*/
bin?:
| string
| Record<string, string>;
| Partial<Record<string, string>>;

/**
Filenames to put in place for the `man` program to find.
Expand Down Expand Up @@ -504,7 +504,7 @@ declare namespace PackageJson {
/**
Indicate peer dependencies that are optional.
*/
peerDependenciesMeta?: Record<string, {optional: true}>;
peerDependenciesMeta?: Partial<Record<string, {optional: true}>>;

/**
Package names that are bundled when the package is published.
Expand All @@ -520,7 +520,7 @@ declare namespace PackageJson {
Engines that this package runs on.
*/
engines?: {
[EngineName in 'npm' | 'node' | string]: string;
[EngineName in 'npm' | 'node' | string]?: string;
};

/**
Expand Down
2 changes: 1 addition & 1 deletion source/tsconfig-json.d.ts
Expand Up @@ -713,7 +713,7 @@ declare namespace TsConfigJson {
/**
Specify path mapping to be computed relative to baseUrl option.
*/
paths?: Record<string, string[]>;
paths?: Partial<Record<string, string[]>>;

/**
List of TypeScript language server plugins to load.
Expand Down
17 changes: 15 additions & 2 deletions test-d/package-json.ts
Expand Up @@ -16,7 +16,7 @@ expectType<PackageJson.Person[] | undefined>(packageJson.contributors);
expectType<PackageJson.Person[] | undefined>(packageJson.maintainers);
expectType<string[] | undefined>(packageJson.files);
expectType<string | undefined>(packageJson.main);
expectType<string | Record<string, string> | undefined>(packageJson.bin);
expectType<string | Partial<Record<string, string>> | undefined>(packageJson.bin);
expectType<string | undefined>(packageJson.types);
expectType<string | undefined>(packageJson.typings);
expectType<string | string[] | undefined>(packageJson.man);
Expand All @@ -36,7 +36,7 @@ expectType<string[] | undefined>(packageJson.bundleDependencies);
expectType<string[] | undefined>(packageJson.bundledDependencies);
expectType<PackageJson.Dependency | undefined>(packageJson.resolutions);
expectType<PackageJson.WorkspaceConfig | string[] | undefined>(packageJson.workspaces);
expectType<Record<string, string> | undefined>(packageJson.engines);
expectType<Partial<Record<string, string>> | undefined>(packageJson.engines);
expectType<boolean | undefined>(packageJson.engineStrict);
expectAssignable<
| undefined
Expand Down Expand Up @@ -67,4 +67,17 @@ expectType<
>(packageJson.esnext);
expectType<PackageJson | undefined>(packageJson.jspm);

// Undefined assigns
expectAssignable<PackageJson.Dependency>({dep: undefined});
expectAssignable<typeof packageJson['engines']>({engine: undefined});
expectAssignable<typeof packageJson['scripts']>({unknownScript: undefined});
expectAssignable<typeof packageJson['bin']>({bin: undefined});
expectAssignable<typeof packageJson['typesVersions']>({
'>=4': {
'*': ['src'],
somethingElse: undefined,
},
'<4': undefined,
});

expectNotAssignable<Record<string, unknown>>(packageJson);
5 changes: 4 additions & 1 deletion test-d/tsconfig-json.ts
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import {expectType, expectAssignable} from 'tsd';
import type {TsConfigJson} from '../index';

const tsConfig: TsConfigJson = {};
Expand All @@ -11,3 +11,6 @@ expectType<string[] | undefined>(tsConfig.files);
expectType<string[] | undefined>(tsConfig.include);
expectType<TsConfigJson.References[] | undefined>(tsConfig.references);
expectType<TsConfigJson.TypeAcquisition | undefined>(tsConfig.typeAcquisition);

// Undefined assigns
expectAssignable<NonNullable<typeof tsConfig['compilerOptions']>['paths']>({path: undefined});

0 comments on commit f2aae51

Please sign in to comment.