Skip to content

Commit f94b983

Browse files
committed
Docs: update README
1 parent 985291d commit f94b983

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ![fffunction](./fffunction.svg)
2-
![typescript](https://img.shields.io/badge/written%20for-typescript-3178c6?style=flat-square) [![codecov](https://img.shields.io/codecov/c/github/adrgautier/fffunction?style=flat-square&token=IPTGBDRRJE)](https://codecov.io/gh/adrgautier/fffunction) ![prettier](https://img.shields.io/badge/code%20style-prettier-ff69b4?style=flat-square) [![npm](https://img.shields.io/npm/v/fffunction?style=flat-square)](https://www.npmjs.com/package/fffunction)
2+
![typescript](https://img.shields.io/badge/written%20for-typescript-3178c6?style=flat-square) ![biome](https://img.shields.io/badge/checked%20with-biome-60a5fa?style=flat-square) [![codecov](https://img.shields.io/codecov/c/github/adrgautier/fffunction?style=flat-square&token=IPTGBDRRJE)](https://codecov.io/gh/adrgautier/fffunction) [![npm](https://img.shields.io/npm/v/fffunction?style=flat-square)](https://www.npmjs.com/package/fffunction)
33

44
**fffunction** is a tool which simplifies **polymorphic** functions declaration and adds **type constraints** to the implemention function.
55

@@ -20,7 +20,7 @@ const random = fffunction
2020
.f<(type: "number") => number>()
2121
.f<(type: "string") => string>()
2222
.f(function ([check, type]) {
23-
if (arg === "number") {
23+
if (type === "number") {
2424
return check(Math.random());
2525
}
2626
return check(uuidv4());
@@ -38,7 +38,7 @@ If the value `"string"` is provided, an uuid will be returned.
3838
console.log(random("string")); // 425dd1a0-cfc0-4eac-a2d7-486860d9bdd4
3939
```
4040

41-
The returned type **is guaranted** by the `_check` function.
41+
The returned type **is guaranted** by the `check` function.
4242

4343
# How to use
4444

@@ -86,7 +86,7 @@ fffunction
8686
.f<(i: { id: number }) => Item>()
8787
.f<(i: { id: number, name: string }) => Profile>()
8888
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
89-
// Type 'Profile' does not satisfy the constraint 'never'. ts(2344)
89+
// Type 'Profile' is not assignable to type 'never'. ts(2344)
9090
```
9191

9292
*Primitive* types **cannot overlap** each others:
@@ -96,7 +96,7 @@ fffunction
9696
.f<(a: `https://${string}`) => URL>()
9797
.f<(a: string) => string>()
9898
^^^^^^^^^^^^^^^^^^^^^
99-
// Type 'string' does not satisfy the constraint 'never'. ts(2344)
99+
// Type 'string' is not assignable to type 'never'. ts(2344)
100100
```
101101

102102
This ensures that each input type can be **narrowed down** later in the function implementation.
@@ -116,7 +116,7 @@ fffunction
116116

117117
This is for two reasons:
118118
- the inference would only work in "overload mode"
119-
- the implementation function **cannot check* if the returned value has the proper subset
119+
- the implementation function **cannot check** if the returned value has the proper subset
120120

121121
## Function implementation
122122

@@ -130,12 +130,12 @@ The implementation of the function is based on the concept of [type narrowing](h
130130
```
131131

132132
The `implementation` function (named here for the example) will receive a tuple. This argument carries:
133-
- the "check" function that ensures the return value is narrowed down enought
133+
- the "check" function that ensures the return value is narrowed down enough
134134
- the argument(s) provided in the same order as declared in the signatures
135135

136136
### Narrowing type
137137

138-
In the main scope of the function, the type of `arg` is **uncertain**. It can be either of the argument types defined in the signatures. We want to create a narrowed scope for each possible type :
138+
In the main scope of the function, the type of `arg` is **uncertain**. It can be either of the argument types defined in the signatures. We want to create a narrowed scope for each possible type:
139139

140140
```ts
141141
function implementation ([check, arg]) {
@@ -162,7 +162,7 @@ function implementation ([check, arg]) {
162162
```
163163

164164
> This function is **mandatory**. You can't return any value without using this method.
165-
> In fact, it must also be called for **void returns** :
165+
> In fact, it must also be called for **void returns**:
166166
> ```ts
167167
> return check();
168168
> ```
@@ -180,7 +180,7 @@ fffunction
180180
if('name' in arg) {
181181
return check('profile');
182182
^^^^^^^^^
183-
// Type 'string' does not satisfy the constraint 'never'. ts(2344)
183+
// Argument of type '"profile"' is not assignable to parameter of type 'never'. ts(2345)
184184
}
185185
return check('item');
186186
});
@@ -201,7 +201,7 @@ fffunction
201201

202202
### Optional "overload" mode
203203

204-
You can enable the **"overload" mode** by passing "overload" to the generic :
204+
You can enable the **"overload" mode** by passing "overload" to the generic:
205205

206206
```ts
207207
.f<'overload'>(implementation);
@@ -219,7 +219,7 @@ This can make the resulting function easier to understand with each signature in
219219

220220
#### Drawback
221221

222-
With this approach you loose the ability to call the function with uncertain input data. E.g. the following is not possible :
222+
With this approach you loose the ability to call the function with uncertain input data. E.g. the following is not possible:
223223

224224
```ts
225225
random(mode as "string" | "number"); // No overload matches this call.
@@ -231,7 +231,7 @@ With the above example, `mode` must be either `"string"` or `"number"`. The unce
231231

232232
## Signature declaration
233233

234-
### `TS2344: Type 'A' does not satisfy the constraint 'never'`
234+
### `TS2344: Type 'A' is not assignable to type 'never'`
235235

236236
```ts
237237
.f<(a: "string") => string>()
@@ -243,7 +243,7 @@ That means input of two signatures are conflicting. See the **input overlapping*
243243

244244
## Function implementation
245245

246-
### `TS2344: Type 'A' does not satisfy the constraint 'never'`
246+
### `TS2345: Argument of type 'A' is not assignable to parameter of type 'never'`
247247

248248
```ts
249249
return check(value);

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ type FFFunction<TSignatures extends AnySignature[] = []> = {
2525
/**
2626
* Implement function.
2727
* @example
28-
* .f(([_check, arg]) => {
28+
* .f(([check, arg]) => {
2929
* if (arg === 'string') {
30-
* return _check(uuidv4());
30+
* return check(uuidv4());
3131
* }
32-
* return _check(Math.random());
32+
* return check(Math.random());
3333
* });
3434
*/
3535
f<TMode extends "overload" | "condition" = "condition">(

tests/index.test-types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { fffunction } from "../src";
1414
fffunction
1515
.f<(a: string) => { test2: "test2" }>()
1616
.f<(b: number) => { test: "test" }>()
17-
.f(function implementation([_check]) {
18-
return _check({ test: "test", test2: "test2" });
17+
.f(function implementation([check]) {
18+
return check({ test: "test", test2: "test2" });
1919
});
2020

2121
fffunction
@@ -31,11 +31,11 @@ fffunction
3131
fffunction
3232
.f<(a: "string") => string>()
3333
.f<(a: "void") => void>()
34-
.f(([_check, arg]) => {
34+
.f(([check, arg]) => {
3535
if (arg === "string") {
36-
return _check("test");
36+
return check("test");
3737
}
38-
return _check();
38+
return check();
3939
});
4040

4141
const overload = fffunction

0 commit comments

Comments
 (0)