Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gunar/ts-with
Enforce the presence of otherwise optional fields in TypeScript.
https://github.com/gunar/ts-with
Last synced: 6 days ago
JSON representation
Enforce the presence of otherwise optional fields in TypeScript.
- Host: GitHub
- URL: https://github.com/gunar/ts-with
- Owner: gunar
- License: mit
- Created: 2019-03-12T17:58:40.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-03-12T18:25:59.000Z (over 5 years ago)
- Last Synced: 2024-09-22T04:18:06.735Z (about 2 months ago)
- Size: 2.93 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Installation
```
npm install --save @gunar/ts-with
```# Summary
This package contains type maps for enforcing the presence of otherwise optional fields.See the [blog post](https://medium.com/@gunar/narrower-optional-fields-for-typescript-49fd68335601) for more context.
## Example
```ts
interface User {
name: string
location?: string
}// This passes type check but shouldn't ---------------------
const getTrimmedLocation = (u: User) => {
if (u.location) return u.location.trim()
else throw Error('User doesnt have a `location` field')
}getTrimmedLocation({ name: 'Gunar' })
// This DOESN'T pass type check, as expected! ---------------
import { With } from '@gunar/ts-with'const getTrimmedLocationSafe: (u: With) =>
u.location.trim()getTrimmedLocationSafe({ user: 'Gunar' })
```