pick-ts
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i pick-ts

Weekly Downloads

0

Version

0.1.1

License

Apache-2.0

Unpacked Size

22.8 kB

Total Files

11

Last publish

Collaborators

  • lostintime