Skip to content

Commit ba7cf96

Browse files
committed
Update README
1 parent 0832fd3 commit ba7cf96

File tree

1 file changed

+109
-75
lines changed

1 file changed

+109
-75
lines changed

README.md

Lines changed: 109 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ The `unknownutil` provides the following predicate functions
2727
- `isNullish(x: unknown): x is null | undefined`
2828
- `isLike<R, T extends unknown>(ref: R, x: unknown, pred?: Predicate<T>): x is R`
2929

30+
The above function can be used to check the type of any variable and guarantee
31+
its type inside a closed `if` scope.
32+
3033
For example:
3134

3235
```typescript
@@ -56,7 +59,7 @@ if (isArray(a, isString)) {
5659
}
5760
```
5861

59-
Use `isLike` if you need some complicated types like tuple or struct like:
62+
Use `isLike` if you need some complicated types like:
6063

6164
```typescript
6265
import { isLike } from "https://deno.land/x/unknownutil/mod.ts";
@@ -89,117 +92,148 @@ if (isLike({ foo: "", bar: 0 }, e)) {
8992
}
9093
```
9194

92-
### ensureXXXXX
95+
### assertXXXXX
96+
97+
The `unknownutil` provides the following assert functions
9398

94-
The `unknownutil` provides the following ensure functions which will raise
95-
`EnsureError` when a given `x` is not expected type.
99+
- `assertString(x: unknown): assert x is string`
100+
- `assertNumber(x: unknown): assert x is number`
101+
- `assertBoolean(x: unknown): assert x is boolean`
102+
- `assertArray<T extends unknown>(x: unknown, pred?: Predicate<T>): assert x is T[]`
103+
- `assertObject<T extends unknown>(x: unknown, pred?: Predicate<T>): assert x is Record<string, T>`
104+
- `assertFunction(x: unknown): assert x is (...args: unknown[]) => unknown`
105+
- `assertNull(x: unknown): assert x is null`
106+
- `assertUndefined(x: unknown): assert x is undefined`
107+
- `assertNullish(x: unknown): assert x is null | undefined`
108+
- `assertLike<R, T extends unknown>(ref: R, x: unknown, pred?: Predicate<T>): assert x is R`
96109

97-
- `ensureString(x: unknown): assert x is string`
98-
- `ensureNumber(x: unknown): assert x is number`
99-
- `ensureBoolean(x: unknown): assert x is boolean`
100-
- `ensureArray<T extends unknown>(x: unknown, pred?: Predicate<T>): assert x is T[]`
101-
- `ensureObject<T extends unknown>(x: unknown, pred?: Predicate<T>): x is Record<string, T>`
102-
- `ensureFunction(x: unknown): x is (...args: unknown[]) => unknown`
103-
- `ensureNull(x: unknown): x is null`
104-
- `ensureUndefined(x: unknown): x is undefined`
105-
- `ensureNullish(x: unknown): x is null | undefined`
110+
The above function can be used to guarantee the type of any variable by throwing
111+
an exception if the type is not expected.
106112

107113
For example:
108114

109115
```typescript
110-
import { ensureString } from "https://deno.land/x/unknownutil/mod.ts";
116+
import { assertString } from "https://deno.land/x/unknownutil/mod.ts";
111117

112-
const a: unknown = "Hello";
113-
ensureString(a); // Now 'a' is 'string'
118+
function say(message: string): void {
119+
console.log(message);
120+
}
114121

122+
const a: unknown = "Hello";
115123
const b: unknown = 0;
116-
ensureString(b); // Raise EnsureError on above while 'b' is not string
124+
125+
// Because 'a' is 'unknown', TypeScript won't allow a code like below
126+
//say(a);
127+
128+
// But once the 'assertString(a)' is passed, TypeScript knows that 'a' is 'string'
129+
// thus it accepts the code that was not accepted before.
130+
assertString(a);
131+
say(a);
132+
133+
// Or raise 'AssertError' if a given value is not string
134+
assertString(b);
135+
say(b);
117136
```
118137

119-
Additionally, `ensureArray` and `ensureObject` supports an inner predicate
120-
function to predicate `x` more precisely like:
138+
More complex type predications are available on `assertXXXXX` as well like
139+
`isXXXXX`.
121140

122-
```typescript
123-
import { ensureArray, isString } from "https://deno.land/x/unknownutil/mod.ts";
141+
### ensureXXXXX
124142

125-
const a: unknown = ["a", "b", "c"];
126-
ensureArray(a); // Now 'a' is 'unknown[]'
127-
ensureArray(a, isString); // Now 'a' is 'string[]'
143+
The `unknownutil` provides the following ensure functions
128144

129-
const b: unknown = [0, 1, 2];
130-
ensureArray(b); // Now 'b' is 'unknown[]'
131-
ensureArray(b, isString); // Raise EnsureError on above while 'b' is not string array
132-
```
145+
- `ensureString(x: unknown): string`
146+
- `ensureNumber(x: unknown): number`
147+
- `ensureBoolean(x: unknown): boolean`
148+
- `ensureArray<T extends unknown>(x: unknown, pred?: Predicate<T>): T[]`
149+
- `ensureObject<T extends unknown>(x: unknown, pred?: Predicate<T>): Record<string, T>`
150+
- `ensureFunction(x: unknown): (...args: unknown[]) => unknown`
151+
- `ensureNull(x: unknown): null`
152+
- `ensureUndefined(x: unknown): undefined`
153+
- `ensureNullish(x: unknown): null | undefined`
154+
- `ensureLike<R, T extends unknown>(ref: R, x: unknown, pred?: Predicate<T>): R`
133155

134-
Use `ensureLike` if you need some complicated types like tuple or struct like:
156+
The above function can be used to guarantee the type of any variable by throwing
157+
an exception if the type is not expected. The difference between assert and
158+
ensure is whether to assert the argument or the return type.
159+
160+
For example:
135161

136162
```typescript
137-
import { ensureLike } from "https://deno.land/x/unknownutil/mod.ts";
163+
import { ensureString } from "https://deno.land/x/unknownutil/mod.ts";
138164

139-
const a: unknown = ["a", "b", "c"];
140-
ensureLike([], a); // Now 'a' is 'unknown[]'
141-
ensureLike(["", "", ""], a); // Now 'a' is '[string, string, string]'
165+
function say(message: string): void {
166+
console.log(message);
167+
}
142168

143-
const b: unknown = { foo: "foo", bar: 0 };
144-
ensureLike({}, b); // Now 'b' is 'Record<string, unknown>'
145-
ensureLike({ foo: "", bar: 0 }, b); // Now 'b' is '{foo: string, bar: number}'
169+
const a: unknown = "Hello";
170+
const b: unknown = 0;
171+
172+
// Because 'a' is 'unknown', TypeScript won't allow a code like below
173+
//say(a);
174+
175+
// But once the 'ensureString(a)' is passed, TypeScript knows that return value is 'string'
176+
// thus it accepts the code.
177+
say(ensureString(a));
178+
179+
// Or raise 'AssertError' if a given value is not string
180+
say(ensureString(b));
146181
```
147182

148-
### assumeXXXXX
183+
More complex type predications are available on `ensureXXXXX` as well like
184+
`isXXXXX`.
149185

150-
The `unknownutil` provides the following assume functions which returns a given
151-
`x` as is or raise `EnsureError` when that is not expected type.
186+
### maybeXXXXX
152187

153-
- `assumeString(x: unknown): string`
154-
- `assumeNumber(x: unknown): number`
155-
- `assumeBoolean(x: unknown): boolean`
156-
- `assumeArray<T extends unknown>(x: unknown, pred?: Predicate<T>): T[]`
157-
- `assumeObject<T extends unknown>(x: unknown, pred?: Predicate<T>): Record<string, T>`
158-
- `assumeFunction(x: unknown): (...args: unknown[]) => unknown`
159-
- `assumeNull(x: unknown): null`
160-
- `assumeUndefined(x: unknown): undefined`
161-
- `assumeNullish(x: unknown): null | undefined`
188+
The `unknownutil` provides the following maybe functions
189+
190+
- `maybeString(x: unknown): string | undefined`
191+
- `maybeNumber(x: unknown): number | undefined`
192+
- `maybeBoolean(x: unknown): boolean | undefined`
193+
- `maybeArray<T extends unknown>(x: unknown, pred?: Predicate<T>): T[] | undefined`
194+
- `maybeObject<T extends unknown>(x: unknown, pred?: Predicate<T>): Record<string, T> | undefined`
195+
- `maybeFunction(x: unknown): ((...args: unknown[]) => unknown) | undefined`
196+
- `maybeLike<R, T extends unknown>(ref: R, x: unknown, pred?: Predicate<T>): R | undefined`
197+
198+
The above function will return `undefined` if the type of any variable is not
199+
expected, so it is possible to give an alternative value using the Nullish
200+
coalescing operator (`??`).
162201

163202
For example:
164203

165204
```typescript
166-
import { assumeString } from "https://deno.land/x/unknownutil/mod.ts";
205+
import { maybeString } from "https://deno.land/x/unknownutil/mod.ts";
167206

168-
const a: unknown = "Hello";
169-
const a1 = assumeString(a); // Now 'a' and 'a1' is 'string'
207+
function say(message: string): void {
208+
console.log(message);
209+
}
170210

211+
const a: unknown = "Hello";
171212
const b: unknown = 0;
172-
const b1 = assumeString(b); // Raise EnsureError on above while 'b' is not string
173-
```
174213

175-
Additionally, `assumeArray` and `assumeObject` supports an inner predicate
176-
function to predicate `x` more precisely like:
214+
// Because 'a' is 'unknown', TypeScript won't allow a code like below
215+
//say(a);
177216

178-
```typescript
179-
import { assumeArray, isString } from "https://deno.land/x/unknownutil/mod.ts";
180-
181-
const a: unknown = ["a", "b", "c"];
182-
const a1 = assumeArray(a); // Now 'a' and 'a1' is 'unknown[]'
183-
const a2 = assumeArray(a, isString); // Now 'a' and 'a2' is 'string[]'
217+
// But the 'maybeString(a)' returns 'string | undefined' thus users can use
218+
// Nullish coalescing operator to give an alternative value to ensure that the
219+
// value given to the 'say()' is 'string'.
220+
// The following code print "Hello" to the console.
221+
say(maybeString(a) ?? "World");
184222

185-
const b: unknown = [0, 1, 2];
186-
const b1 = assumeArray(b); // Now 'b' and 'b1' is 'unknown[]'
187-
const b2 = assumeArray(b, isString); // Raise EnsureError on above while 'b' is not string array
223+
// The following code print "World" to the console.
224+
say(maybeString(b) ?? "World");
188225
```
189226

190-
Use `assumeLike` if you need some complicated types like tuple or struct like:
227+
More complex type predications are available on `maybeXXXXX` as well like
228+
`isXXXXX`.
191229

192-
```typescript
193-
import { assumeLike } from "https://deno.land/x/unknownutil/mod.ts";
194-
195-
const a: unknown = ["a", "b", "c"];
196-
const a1 = assumeLike([], a); // Now 'a' and 'a1' is 'unknown[]'
197-
const a2 = assumeLike(["", "", ""], a); // Now 'a' and 'a2' is '[string, string, string]'
230+
## Migration from v1 to v2
198231

199-
const b: unknown = { foo: "foo", bar: 0 };
200-
const b1 = assumeLike({}, b); // Now 'b' and 'b1' is 'Record<string, unknown>'
201-
const b2 = assumeLike({ foo: "", bar: 0 }, b); // Now 'b' and 'b2' is '{foo: string, bar: number}'
202-
```
232+
1. Replace `ensure` or `assert` to corresponding specific functions (e.g.
233+
`ensureString` or `assertNumber`)
234+
2. Rename `xxxxxNone` to `xxxxxNullish` (e.g. `isNone` to `isNullish`)
235+
3. Rename `ensureXXXXX` to `assertXXXXX` (e.g. `ensureString` to `assertString`)
236+
4. Rename `assumeXXXXX` to `ensureXXXXX` (e.g. `assumeNumber` to `ensureNumber`)
203237

204238
## Development
205239

0 commit comments

Comments
 (0)