0.1.1 • Published 6 years ago

pick-ts v0.1.1

Weekly downloads
1
License
Apache-2.0
Repository
github
Last release
6 years ago

Pick-TS

Travis Coverage Status Greenkeeper badge

A best-effort typesafe(er1) function to pick selected object fields.

import pick from "pick-ts"

const person = {
  name: "John",
  age: 20
}

type Named = {
  name: string
}

// These lines works
const n: Named = pick("name")(person)
const p: Person = pick("name", "age")(person)

// But this will fail at compilation time
const t = pick("title")(person)
const z: Named = pick("title")({ title: "Aloha" })

Limitations

  1. As picked type is infered from pick argument value types - setting explicit keys type will break result type:
type Person = {
  name: string
  age: number
}

// This will compile, but doesn't work as expected!
const p: Person = pick<keyof Person>()({ name: "John", age: 20 })

Related Links

Contribute

Perfection is Achieved Not When There Is Nothing More to Add, But When There Is Nothing Left to Take Away

Fork, Contribute, Push, Create pull request, Thanks.