Skip to content

Commit 0f273d7

Browse files
BendingBendersindresorhus
authored andcommitted
Add PackageJson and LiteralUnion types (#5)
1 parent 7934702 commit 0f273d7

File tree

6 files changed

+602
-1
lines changed

6 files changed

+602
-1
lines changed

index.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export {PackageJson} from './source/package-json';
2+
13
// TODO: Add more examples
24

35
// TODO: This can just be `export type Primitive = not object` when the `not` keyword is out.
@@ -95,3 +97,33 @@ const ab: Merge<Foo, Bar> = {a: 1, b: 2};
9597
```
9698
*/
9799
export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
100+
101+
/**
102+
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.
103+
104+
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.
105+
106+
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.
107+
108+
@example
109+
```
110+
import {LiteralUnion} from 'type-fest';
111+
112+
type Pet = 'dog' | 'cat' | string;
113+
114+
const pet: Pet = '';
115+
// Start typing in your TypeScript-enabled IDE.
116+
// You **will not** get auto-completion for `dog` and `cat` literals.
117+
118+
// …
119+
120+
type Pet2 = LiteralUnion<'dog' | 'cat', string>;
121+
122+
const pet: Pet2 = '';
123+
// You **will** get auto-completion for `dog` and `cat` literals.
124+
```
125+
*/
126+
export type LiteralUnion<
127+
LiteralType extends BaseType,
128+
BaseType extends Primitive
129+
> = LiteralType | (BaseType & {_?: never});

index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable import/no-unassigned-import */
22
import './test/omit';
33
import './test/merge';
4+
import './test/package-json';
45

56
// TODO: Add negative tests. Blocked by: https://github.com/SamVerschueren/tsd-check/issues/2

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"test": "xo && tsd-check"
1717
},
1818
"files": [
19-
"index.d.ts"
19+
"index.d.ts",
20+
"source"
2021
],
2122
"keywords": [
2223
"typescript",

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ See the [types file](index.d.ts) for complete docs.
6464

6565
- `Omit` - Create a type from an object type without certain keys.
6666
- `Merge` - Merge two types into a new type. Keys of the second type overrides keys of the first type.
67+
- `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).
68+
69+
### Miscellaneous
70+
71+
- `PackageJson` - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file).
6772

6873

6974
## Declined types

0 commit comments

Comments
 (0)