@gunar/ts-with
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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 for more context.

Example

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<User, 'location') =>
  u.location.trim()

getTrimmedLocationSafe({ user: 'Gunar' })

Readme

Keywords

Package Sidebar

Install

npm i @gunar/ts-with

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

2.7 kB

Total Files

4

Last publish

Collaborators

  • gunar