https://github.com/alloc/option-types
Utility types for options-related TypeScript definitions
https://github.com/alloc/option-types
typescript
Last synced: 8 days ago
JSON representation
Utility types for options-related TypeScript definitions
- Host: GitHub
- URL: https://github.com/alloc/option-types
- Owner: alloc
- Created: 2024-11-18T18:00:10.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-26T17:51:00.000Z (over 1 year ago)
- Last Synced: 2025-10-25T03:55:00.137Z (5 months ago)
- Topics: typescript
- Language: TypeScript
- Homepage:
- Size: 11.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# option-types
Utility types for options-related TypeScript definitions.
This package doesn't have any runtime code. It only contains types.
### Motivation
Using [`exactOptionalPropertyTypes`](https://www.typescriptlang.org/tsconfig/#exactOptionalPropertyTypes) in TypeScript is great, except when you have a big object type with many optional properties. Manually adding `undefined` to every optional property type is annoying and error-prone.
Using conditional types and mapped types, we can automatically add `undefined` to every optional property type.
## Types
### `Option`
Any type wrapped in `Option` can be assigned an `undefined` value. If the wrapped type is an object, its optional properties can also be assigned an `undefined` value. This type is recursive.
### `Options`
When an object type is itself required, but it contains optional properties, this type can be used to ensure that the optional properties can be assigned an `undefined` value. This type is recursive, so nested object types are also affected.
### `OptionalKeys`
Extract the keys of an object type that are optional. Note that a key is only considered “optional” if it was defined with a `?` in the type definition.
### `ShallowOptions`
Ensure each optional property can be assigned an `undefined` value. Nested object types are not affected.