diff --git a/index.d.ts b/index.d.ts index dc9d8774f..e6328d39b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,5 @@ +export {PackageJson} from './source/package-json'; + // TODO: Add more examples // TODO: This can just be `export type Primitive = not object` when the `not` keyword is out. @@ -95,3 +97,33 @@ const ab: Merge = {a: 1, b: 2}; ``` */ export type Merge = Omit> & SecondType; + +/** +Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. + +Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals. + +This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore. + +@example +``` +import {LiteralUnion} from 'type-fest'; + +type Pet = 'dog' | 'cat' | string; + +const pet: Pet = ''; +// Start typing in your TypeScript-enabled IDE. +// You **will not** get auto-completion for `dog` and `cat` literals. + +// … + +type Pet2 = LiteralUnion<'dog' | 'cat', string>; + +const pet: Pet2 = ''; +// You **will** get auto-completion for `dog` and `cat` literals. +``` + */ +export type LiteralUnion< + LiteralType extends BaseType, + BaseType extends Primitive +> = LiteralType | (BaseType & {_?: never}); diff --git a/index.test-d.ts b/index.test-d.ts index a7e927bbb..a4cc5505b 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,6 @@ /* eslint-disable import/no-unassigned-import */ import './test/omit'; import './test/merge'; +import './test/package-json'; // TODO: Add negative tests. Blocked by: https://github.com/SamVerschueren/tsd-check/issues/2 diff --git a/package.json b/package.json index f593a469c..24b01ee52 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "test": "xo && tsd-check" }, "files": [ - "index.d.ts" + "index.d.ts", + "source" ], "keywords": [ "typescript", diff --git a/readme.md b/readme.md index 1ae1e68d8..38711655b 100644 --- a/readme.md +++ b/readme.md @@ -64,6 +64,11 @@ See the [types file](index.d.ts) for complete docs. - `Omit` - Create a type from an object type without certain keys. - `Merge` - Merge two types into a new type. Keys of the second type overrides keys of the first type. +- `LiteralUnion` - Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). + +### Miscellaneous + +- `PackageJson` - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). ## Declined types diff --git a/source/package-json.d.ts b/source/package-json.d.ts new file mode 100644 index 000000000..4c83c7362 --- /dev/null +++ b/source/package-json.d.ts @@ -0,0 +1,494 @@ +import {LiteralUnion} from '..'; + +declare namespace PackageJson { + /** + A person who has been involved in creating or maintaining the package. + */ + export type Person = + | string + | { + name: string; + url?: string; + email?: string; + }; + + export type BugsLocation = + | string + | { + /** + The URL to the package's issue tracker. + */ + url?: string; + + /** + The email address to which issues should be reported. + */ + email?: string; + }; + + export interface DirectoryLocations { + /** + Location for executable scripts. Sugar to generate entries in the `bin` property by walking the folder. + */ + bin?: string; + + /** + Location for Markdown files. + */ + doc?: string; + + /** + Location for example scripts. + */ + example?: string; + + /** + Location for the bulk of the library. + */ + lib?: string; + + /** + Location for man pages. Sugar to generate a `man` array by walking the folder. + */ + man?: string; + + /** + Location for test files. + */ + test?: string; + + [directoryType: string]: unknown; + } + + export type Scripts = { + /** + Run **before** the package is published (Also run on local `npm install` without any arguments). + */ + prepublish?: string; + + /** + Run both **before** the package is packed and published, and on local `npm install` without any arguments. This is run **after** `prepublish`, but **before** `prepublishOnly`. + */ + prepare?: string; + + /** + Run **before** the package is prepared and packed, **only** on `npm publish`. + */ + prepublishOnly?: string; + + /** + Run **before** a tarball is packed (on `npm pack`, `npm publish`, and when installing git dependencies). + */ + prepack?: string; + + /** + Run **after** the tarball has been generated and moved to its final destination. + */ + postpack?: string; + + /** + Run **after** the package is published. + */ + publish?: string; + + /** + Run **after** the package is published. + */ + postpublish?: string; + + /** + Run **before** the package is installed. + */ + preinstall?: string; + + /** + Run **after** the package is installed. + */ + install?: string; + + /** + Run **after** the package is installed and after `install`. + */ + postinstall?: string; + + /** + Run **before** the package is uninstalled and before `uninstall`. + */ + preuninstall?: string; + + /** + Run **before** the package is uninstalled. + */ + uninstall?: string; + + /** + Run **after** the package is uninstalled. + */ + postuninstall?: string; + + /** + Run **before** bump the package version and before `version`. + */ + preversion?: string; + + /** + Run **before** bump the package version. + */ + version?: string; + + /** + Run **after** bump the package version. + */ + postversion?: string; + + /** + Run with the `npm test` command, before `test`. + */ + pretest?: string; + + /** + Run with the `npm test` command. + */ + test?: string; + + /** + Run with the `npm test` command, after `test`. + */ + posttest?: string; + + /** + Run with the `npm stop` command, before `stop`. + */ + prestop?: string; + + /** + Run with the `npm stop` command. + */ + stop?: string; + + /** + Run with the `npm stop` command, after `stop`. + */ + poststop?: string; + + /** + Run with the `npm start` command, before `start`. + */ + prestart?: string; + + /** + Run with the `npm start` command. + */ + start?: string; + + /** + Run with the `npm start` command, after `start`. + */ + poststart?: string; + + /** + Run with the `npm restart` command, before `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided. + */ + prerestart?: string; + + /** + Run with the `npm restart` command. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided. + */ + restart?: string; + + /** + 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; + } & { + [scriptName: 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 interface Dependency { + [packageName: string]: string; + } + + export interface NonStandardEntryPoints { + /** + An ECMAScript module ID that is the primary entry point to the program. + */ + module?: string; + + /** + A module ID with untranspiled code that is the primary entry point to the program. + */ + esnext?: + | string + | { + main?: string; + browser?: string; + [moduleName: string]: string | undefined; + }; + + /** + A hint to JavaScript bundlers or component tools when packaging modules for client side use. + */ + browser?: + | string + | { + [moduleName: string]: string | false; + }; + } + + export interface TypeScriptConfiguration { + /** + Location of the bundled TypeScript declaration file. + */ + types?: string; + + /** + Location of the bundled TypeScript declaration file. Alias of `types`. + */ + typings?: string; + } + + export interface YarnConfiguration { + /** + Selective version resolutions. Allows the definition of custom package versions inside dependencies without manual edits in the `yarn.lock` file. + */ + resolutions?: Dependency; + } + + export interface JSPMConfiguration { + /** + JSPM configuration. + */ + jspm?: PackageJson; + } +} + +/** +Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Also includes types for fields used by other popular projects, like TypeScript and Yarn. +*/ +export type PackageJson = { + /** + The name of the package. + */ + name?: string; + + /** + Package version, parseable by [`node-semver`](https://github.com/npm/node-semver). + */ + version?: string; + + /** + Package description, listed in `npm search`. + */ + description?: string; + + /** + Keywords associated with package, listed in `npm search`. + */ + keywords?: string[]; + + /** + The URL to the package's homepage. + */ + homepage?: LiteralUnion<'.', string>; + + /** + The URL to the package's issue tracker and/or the email address to which issues should be reported. + */ + bugs?: PackageJson.BugsLocation; + + /** + The license for the package. + */ + license?: string; + + /** + The licenses for the package. + */ + licenses?: { + type?: string; + url?: string; + }[]; + + author?: PackageJson.Person; + + /** + A list of people who contributed to the package. + */ + contributors?: PackageJson.Person[]; + + /** + A list of people who maintain the package. + */ + maintainers?: PackageJson.Person[]; + + /** + The files included in the package. + */ + files?: string[]; + + /** + The module ID that is the primary entry point to the program. + */ + main?: string; + + /** + The executable files that should be installed into the `PATH`. + */ + bin?: + | string + | { + [binary: string]: string; + }; + + /** + Filenames to put in place for the `man` program to find. + */ + man?: string | string[]; + + /** + Indicates the structure of the package. + */ + directories?: PackageJson.DirectoryLocations; + + /** + Location for the code repository. + */ + repository?: + | string + | { + type: string; + url: string; + }; + + /** + Script commands that are run at various times in the lifecycle of the package. The key is the lifecycle event, and the value is the command to run at that point. + */ + scripts?: PackageJson.Scripts; + + /** + Is used to set configuration parameters used in package scripts that persist across upgrades. + */ + config?: { + [configKey: string]: unknown; + }; + + /** + The dependencies of the package. + */ + dependencies?: PackageJson.Dependency; + + /** + Additional tooling dependencies that are not required for the package to work. Usually test, build, or documentation tooling. + */ + devDependencies?: PackageJson.Dependency; + + /** + Dependencies that are skipped if they fail to install. + */ + optionalDependencies?: PackageJson.Dependency; + + /** + Dependencies that will usually be required by the package user directly or via another dependency. + */ + peerDependencies?: PackageJson.Dependency; + + /** + Package names that are bundled when the package is published. + */ + bundledDependencies?: string[]; + + /** + Alias of `bundledDependencies`. + */ + bundleDependencies?: string[]; + + /** + Engines that this package runs on. + */ + engines?: { + [EngineName in 'npm' | 'node' | string]: string; + }; + + /** + @deprecated + */ + engineStrict?: boolean; + + /** + Operating systems the module runs on. + */ + os?: LiteralUnion< + | 'aix' + | 'darwin' + | 'freebsd' + | 'linux' + | 'openbsd' + | 'sunos' + | 'win32' + | '!aix' + | '!darwin' + | '!freebsd' + | '!linux' + | '!openbsd' + | '!sunos' + | '!win32', + string + >[]; + + /** + CPU architectures the module runs on. + */ + cpu?: LiteralUnion< + | 'arm' + | 'arm64' + | 'ia32' + | 'mips' + | 'mipsel' + | 'ppc' + | 'ppc64' + | 's390' + | 's390x' + | 'x32' + | 'x64' + | '!arm' + | '!arm64' + | '!ia32' + | '!mips' + | '!mipsel' + | '!ppc' + | '!ppc64' + | '!s390' + | '!s390x' + | '!x32' + | '!x64', + string + >[]; + + /** + If set to `true`, a warning will be shown if package is installed locally. Useful if the package is primarily a command-line application that should be installed globally. + + @deprecated + */ + preferGlobal?: boolean; + + /** + If set to `true`, then npm will refuse to publish it. + */ + private?: boolean; + + /** + * A set of config values that will be used at publish-time. It's especially handy to set the tag, registry or access, to ensure that a given package is not tagged with 'latest', published to the global public registry or that a scoped module is private by default. + */ + publishConfig?: { + [config: string]: unknown; + }; +} & +PackageJson.NonStandardEntryPoints & +PackageJson.TypeScriptConfiguration & +PackageJson.YarnConfiguration & +PackageJson.JSPMConfiguration & { + [key: string]: unknown; +}; diff --git a/test/package-json.ts b/test/package-json.ts new file mode 100644 index 000000000..da3be1492 --- /dev/null +++ b/test/package-json.ts @@ -0,0 +1,68 @@ +import {expectType} from 'tsd-check'; +import {PackageJson, LiteralUnion} from '..'; + +const packageJson: PackageJson = {}; + +expectType(packageJson.name); +expectType(packageJson.version); +expectType(packageJson.description); +expectType(packageJson.keywords); +expectType | undefined>(packageJson.homepage); +expectType(packageJson.bugs); +expectType(packageJson.license); +expectType<{type?: string; url?: string}[] | undefined>(packageJson.licenses); +expectType(packageJson.author); +expectType(packageJson.contributors); +expectType(packageJson.maintainers); +expectType(packageJson.files); +expectType(packageJson.main); +expectType(packageJson.bin); +expectType(packageJson.types); +expectType(packageJson.typings); +expectType(packageJson.man); +expectType(packageJson.directories); +expectType<{type: string; url: string} | string | undefined>( + packageJson.repository +); +expectType(packageJson.scripts); +expectType<{[configKey: string]: unknown} | undefined>(packageJson.config); +expectType(packageJson.dependencies); +expectType(packageJson.devDependencies); +expectType( + packageJson.optionalDependencies +); +expectType(packageJson.peerDependencies); +expectType(packageJson.bundleDependencies); +expectType(packageJson.bundledDependencies); +expectType(packageJson.resolutions); +expectType<{[engineName: string]: string} | undefined>(packageJson.engines); +expectType(packageJson.engineStrict); +expectType< + | undefined + | LiteralUnion< + 'darwin' | 'linux' | 'win32' | '!darwin' | '!linux' | '!win32', + string + >[] +>(packageJson.os); +expectType< + | undefined + | LiteralUnion< + 'x64' | 'ia32' | 'arm' | 'mips' | '!x64' | '!ia32' | '!arm' | '!mips', + string + >[] +>(packageJson.cpu); +expectType(packageJson.preferGlobal); +expectType(packageJson.private); +expectType<{[config: string]: unknown} | undefined>(packageJson.publishConfig); +expectType(packageJson.module); +expectType< + | string + | { + main?: string; + browser?: string; + [moduleName: string]: string | undefined; + } + | undefined +>(packageJson.esnext); +expectType(packageJson.jspm); +expectType(packageJson.foo);