Skip to content

Commit

Permalink
feat: add Primitive and Falsey (#70)
Browse files Browse the repository at this point in the history
As requested in #69 (comment)
  • Loading branch information
ersimont authored and piotrwitek committed Apr 2, 2019
1 parent 868b599 commit f7e4ff8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<A, B>`](#setintersectiona-b-same-as-extract)
Expand Down Expand Up @@ -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<T>`
Expand Down
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ exports[`DeepRequired testType<ReturnType<DeepRequired<NestedFunctionProps>['fir
exports[`Diff testType<Diff<Props, NewProps>>() 1`] = `"Pick<Props, \\"name\\" | \\"visible\\">"`;
exports[`Falsey testType<Falsey>() 1`] = `"Falsey"`;
exports[`FunctionKeys testType<FunctionKeys<MixedProps>>() 1`] = `"\\"setName\\""`;
exports[`Intersection testType<Intersection<Props | NewProps, DefaultProps>>() 1`] = `"Pick<Props, \\"age\\"> | Pick<NewProps, \\"age\\">"`;
Expand All @@ -104,6 +106,8 @@ exports[`Overwrite testType<Overwrite<Props, NewProps>>() 1`] = `"Pick<Pick<Prop
exports[`PickByValue testType<PickByValue<Props, string | number>>() 1`] = `"Pick<Props, \\"name\\" | \\"age\\">"`;
exports[`Primitive testType<Primitive>() 1`] = `"Primitive"`;
exports[`PromiseType testType<PromiseType<Promise<string>>>() 1`] = `"string"`;
exports[`ReadonlyKeys testType<ReadonlyKeys<ReadWriteProps>>() 1`] = `"\\"a\\""`;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export {
DeepNonNullable,
DeepPartial,
Diff,
Falsey,
FunctionKeys,
Intersection,
NonFunctionKeys,
NonUndefined,
Omit,
Overwrite,
Primitive,
PromiseType,
SetComplement,
SetDifference,
Expand Down
14 changes: 14 additions & 0 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { testType } from '../utils/test-utils';
import {
Primitive,
Falsey,
SetIntersection,
SetDifference,
SetComplement,
Expand Down Expand Up @@ -48,6 +50,18 @@ type ReadWriteProps = { readonly a: number; b: string };
* Tests
*/

// @dts-jest:group Primitive
it('Primitive', () => {
// @dts-jest:pass:snap
testType<Primitive>();
});

// @dts-jest:group Falsey
it('Falsey', () => {
// @dts-jest:pass:snap
testType<Falsey>();
});

// @dts-jest:group SetIntersection
it('SetIntersection', () => {
// @dts-jest:pass:snap
Expand Down
20 changes: 19 additions & 1 deletion src/mapped-types.ts
Original file line number Diff line number Diff line change
@@ -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<string | object, Primitive>
*/
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`
Expand Down

0 comments on commit f7e4ff8

Please sign in to comment.