From 701752e541b31215571ff5c0579c1c389c14c3f4 Mon Sep 17 00:00:00 2001 From: ersimont <8042088+ersimont@users.noreply.github.com> Date: Sat, 2 Mar 2019 09:29:43 -0600 Subject: [PATCH] feat: add `Primitive` and `Falsey` --- README.md | 21 ++++++++++++++++ package-lock.json | 28 +++++++++++++++------ src/__snapshots__/mapped-types.spec.ts.snap | 4 +++ src/index.ts | 2 ++ src/mapped-types.spec.ts | 14 +++++++++++ src/mapped-types.ts | 20 ++++++++++++++- 6 files changed, 81 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e4c02ba..781ffc9 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,11 @@ Issues can be funded by anyone and the money will be transparently distributed t # Table of Contents +## Aliases + +* [`Primitive`](#primitive) +* [`Falsey`](#falsey) + ## Operations on sets * [`SetIntersection`](#setintersectiona-b-same-as-extract) @@ -209,6 +214,22 @@ Extract subset `B` from set `A` --- +## Aliases + +### `Primitive` + +The primitive types in TypeScript + +[⇧ back to top](#aliases) + +### `Falsey` + +The falsey values in TypeScript, except NaN which cannot be represented as a type literal + +[⇧ back to top](#aliases) + +--- + ## Operations on objects ### `FunctionKeys` diff --git a/package-lock.json b/package-lock.json index d7695c7..da65aa3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1770,12 +1770,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -1790,17 +1792,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -1917,7 +1922,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -1929,6 +1935,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -1943,6 +1950,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -1950,12 +1958,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -1974,6 +1984,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -2054,7 +2065,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -2066,6 +2078,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -2187,6 +2200,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", diff --git a/src/__snapshots__/mapped-types.spec.ts.snap b/src/__snapshots__/mapped-types.spec.ts.snap index 874b5d5..0c42ce4 100644 --- a/src/__snapshots__/mapped-types.spec.ts.snap +++ b/src/__snapshots__/mapped-types.spec.ts.snap @@ -82,6 +82,8 @@ exports[`DeepRequired testType['fir exports[`Diff testType>() 1`] = `"Pick"`; +exports[`Falsey testType() 1`] = `"Falsey"`; + exports[`FunctionKeys testType>() 1`] = `"\\"setName\\""`; exports[`Intersection testType>() 1`] = `"Pick | Pick"`; @@ -104,6 +106,8 @@ exports[`Overwrite testType>() 1`] = `"Pick>() 1`] = `"Pick"`; +exports[`Primitive testType() 1`] = `"Primitive"`; + exports[`PromiseType testType>>() 1`] = `"string"`; exports[`ReadonlyKeys testType>() 1`] = `"\\"a\\""`; diff --git a/src/index.ts b/src/index.ts index c76d054..8276164 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,12 +24,14 @@ export { DeepNonNullable, DeepPartial, Diff, + Falsey, FunctionKeys, Intersection, NonFunctionKeys, NonUndefined, Omit, Overwrite, + Primitive, PromiseType, SetComplement, SetDifference, diff --git a/src/mapped-types.spec.ts b/src/mapped-types.spec.ts index ddc08cb..4b3af1c 100644 --- a/src/mapped-types.spec.ts +++ b/src/mapped-types.spec.ts @@ -1,5 +1,7 @@ import { testType } from '../utils/test-utils'; import { + Primitive, + Falsey, SetIntersection, SetDifference, SetComplement, @@ -48,6 +50,18 @@ type ReadWriteProps = { readonly a: number; b: string }; * Tests */ +// @dts-jest:group Primitive +it('Primitive', () => { + // @dts-jest:pass:snap + testType(); +}); + +// @dts-jest:group Falsey +it('Falsey', () => { + // @dts-jest:pass:snap + testType(); +}); + // @dts-jest:group SetIntersection it('SetIntersection', () => { // @dts-jest:pass:snap diff --git a/src/mapped-types.ts b/src/mapped-types.ts index b17cc1c..d0b39e1 100644 --- a/src/mapped-types.ts +++ b/src/mapped-types.ts @@ -1,8 +1,26 @@ /** - * Credits to all the people who given inspiration and shared some very usefull code snippets + * Credits to all the people who given inspiration and shared some very useful code snippets * in the following github issue: https://github.com/Microsoft/TypeScript/issues/12215 */ +/** + * Primitive + * @desc The primitive types in TypeScript + * @example + * // Expect: object + * // type ResultStripPrimitives = Exclude + */ +export type Primitive = number | boolean | string | symbol; + +/** + * Falsey + * @desc The falsey values in TypeScript, except NaN + * @example + * // Expect: "a" | "b" + * // type ResultCompact = Exclude<'a' | 'b' | undefined | false, Falsey>; + */ +export type Falsey = null | undefined | false | 0 | ''; + /** * SetIntersection (same as Extract) * @desc Set intersection of given union types `A` and `B`