https://github.com/lostintime/pick-ts
Pick selected object fields
https://github.com/lostintime/pick-ts
Last synced: 10 months ago
JSON representation
Pick selected object fields
- Host: GitHub
- URL: https://github.com/lostintime/pick-ts
- Owner: lostintime
- License: apache-2.0
- Archived: true
- Created: 2018-10-02T16:41:56.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-10-02T16:52:25.000Z (almost 8 years ago)
- Last Synced: 2025-01-16T21:49:25.321Z (over 1 year ago)
- Language: TypeScript
- Size: 40 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Pick-TS
=======
[](https://travis-ci.org/lostintime/pick-ts)
[](https://codecov.io/gh/lostintime/pick-ts?branch=master)
[](https://greenkeeper.io/)
A _best-effort_ typesafe(er1) function to _pick_ selected object fields.
```typescript
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:
```typescript
type Person = {
name: string
age: number
}
// This will compile, but doesn't work as expected!
const p: Person = pick()({ name: "John", age: 20 })
```
### Related Links
* https://github.com/Microsoft/TypeScript/issues/13298
## 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.