Skip to content

Commit

Permalink
Add more detail about optional value casting
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelLefkowitz committed Jul 14, 2024
1 parent 04b73ee commit 65febfa
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ isPerson({ name: "Joel" }) >>

Strictly speaking, `{ name: optional(isString) }` implies that the interface is `{ name: string | undefined }` which allows name to be explicitly undefined. Since this is never useful the interface is interpreted as being `{ name?: string }` which is simpler than having a separate function for strictly optional values.

Note that there has been lots of discussion around changing the way TypeScript handles undefined vs optional parameters: https://github.com/Microsoft/TypeScript/issues/12400. Rather than implementing some casts under the hood to fight the type checker this library leaves it up to the developer to be explicit:

```ts
const isPerson = isRecordOf<Person>({ name: optional(isString) });
```

Whereas this will throw an error when strict optional checking is enabled:

```ts
const isPerson: Validator<Person> = isRecordOf({ name: optional(isString) });
```

### Using literals

Array literals can be converted directly to validators:
Expand Down

0 comments on commit 65febfa

Please sign in to comment.