Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🚀 Add JsonValue utility type #392

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ npm install --save-dev ts-essentials
### Basic

- [`Builtin`](/lib/built-in) - Matches primitive, function, date, error or regular expression
- [`JsonArray`](/lib/json-primitive) - Matches any [JSON array](https://www.rfc-editor.org/rfc/rfc8259#section-3)
- [`JsonObject`](/lib/json-object) - Matches any [JSON object](https://www.rfc-editor.org/rfc/rfc8259#section-3)
- [`JsonPrimitive`](/lib/json-primitive) - Matches any
[JSON primitive value](https://www.rfc-editor.org/rfc/rfc8259#section-3)
- [`JsonValue`](/lib/json-value) - Matches any [JSON value](https://www.rfc-editor.org/rfc/rfc8259#section-3)
- [`KeyofBase`](/lib/key-of-base) -
[`keyofStringsOnly`](https://www.typescriptlang.org/tsconfig#keyofStringsOnly)-tolerant analogue for `PropertyKey`
- [`Prettify<Type>`](/lib/prettify/) - flattens type and makes it more readable on the hover in your IDE
Expand Down
6 changes: 5 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Basic

export * from "./primitive";
export * from "./built-in";
export * from "./json-array";
export * from "./json-object";
export * from "./json-primitive";
export * from "./json-value";
export * from "./key-of-base";
export * from "./primitive";
export * from "./strict-exclude";
export * from "./strict-extract";
export * from "./strict-omit";
Expand Down
16 changes: 16 additions & 0 deletions lib/json-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Matches any [JSON array](https://www.rfc-editor.org/rfc/rfc8259#section-3)

```ts
let jsonArray: JsonArray;

jsonArray = [{ numberProperty: 1 }];
jsonArray = [{ stringProperty: "1" }];
jsonArray = [{ booleanProperty: true }];
// @ts-expect-error: Type 'symbol' is not assignable to type `JsonValue | JsonPrimitive`
jsonArray = [{ symbolProperty: Symbol.iterator }];
jsonArray = [{ nullProperty: null }];
// @ts-expect-error: Type 'undefined' is not assignable to type `JsonValue`
jsonArray = [{ undefinedProperty: undefined }];
```

TS Playground - https://tsplay.dev/N5dVPw
3 changes: 3 additions & 0 deletions lib/json-array/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { JsonValue } from "../json-value";

export type JsonArray = JsonValue[];
30 changes: 30 additions & 0 deletions lib/json-object/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Matches any [JSON object](https://www.rfc-editor.org/rfc/rfc8259#section-3)

```ts
let jsonObject: JsonObject;

jsonObject = { numberProperty: 1 };
jsonObject = { stringProperty: "1" };
jsonObject = { booleanProperty: true };
// @ts-expect-error: Type 'symbol' is not assignable to type `JsonValue | JsonPrimitive`
jsonObject = { symbolProperty: Symbol.iterator };
jsonObject = { nullProperty: null };
// @ts-expect-error: Type 'undefined' is not assignable to type `JsonValue | JsonPrimitive`
jsonObject = { undefinedProperty: undefined };
```

It accepts optional properties, such as `pets?: string[];`:

```ts
type Person = {
name: string;
age: number;
pets?: string[];
};

declare const person: Person;

jsonObject = person;
```

TS Playground - https://tsplay.dev/mZKV1W
8 changes: 8 additions & 0 deletions lib/json-object/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { JsonValue } from "../json-value";

export type JsonObject = {
[Key in string]: JsonValue;
} & {
// optional (non-undefined) properties are acceptable
[Key in string]?: JsonValue;
};
16 changes: 16 additions & 0 deletions lib/json-primitive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Matches any [JSON primitive value](https://www.rfc-editor.org/rfc/rfc8259#section-3)

```ts
let primitive: JsonPrimitive;

primitive = 1;
primitive = "1";
primitive = true;
// Type 'unique symbol' is not assignable to type 'JsonPrimitive'
primitive = Symbol.iterator;
primitive = null;
// Type 'undefined' is not assignable to type 'JsonPrimitive'
primitive = undefined;
```

TS Playground - https://tsplay.dev/WP2ReN
1 change: 1 addition & 0 deletions lib/json-primitive/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type JsonPrimitive = false | null | number | string | true;
36 changes: 36 additions & 0 deletions lib/json-value/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Matches any [JSON value](https://www.rfc-editor.org/rfc/rfc8259#section-3)

```ts
let jsonValue: JsonValue;

// primitive

jsonValue = 1;
jsonValue = "1";
jsonValue = true;
jsonValue = null;

// object

jsonValue = { numberProperty: 1 };
jsonValue = { stringProperty: "1" };
jsonValue = { booleanProperty: true };
// @ts-expect-error: Type 'symbol' is not assignable to type `JsonValue | JsonPrimitive`
jsonValue = { symbolProperty: Symbol.iterator };
jsonValue = { nullProperty: null };
// @ts-expect-error: Type 'undefined' is not assignable to type `JsonValue | JsonPrimitive`
jsonValue = { undefinedProperty: undefined };

// array

jsonValue = [{ numberProperty: 1 }];
jsonValue = [{ stringProperty: "1" }];
jsonValue = [{ booleanProperty: true }];
// @ts-expect-error: Type 'symbol' is not assignable to type `JsonValue | JsonPrimitive`
jsonValue = [{ symbolProperty: Symbol.iterator }];
jsonValue = [{ nullProperty: null }];
// @ts-expect-error: Type 'undefined' is not assignable to type `JsonValue`
jsonValue = [{ undefinedProperty: undefined }];
```

TS Playground - https://tsplay.dev/ND0ARN
5 changes: 5 additions & 0 deletions lib/json-value/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { JsonArray } from "../json-array";
import { JsonObject } from "../json-object";
import { JsonPrimitive } from "../json-primitive";

export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
Loading