gcanti / typelevel-ts

Type level programming in TypeScript
https://gcanti.github.io/typelevel-ts
MIT License
357 stars 12 forks source link

New Feature: add KeysOfType #27

Closed mattiamanzati closed 6 years ago

mattiamanzati commented 6 years ago

This PR implements KeysOfType<A extends object, B> which will return all the keys where the value extends a given one. This can be useful to implement things like Clean<T> which removes from an object the keys where the value is never

type Clean<T> = Pick<T, Exclude<keyof T, KeysOfType<T, never>>>
type TestClean = Clean<{a: string, b: never}> // => {a: string}

another real world usage is to discriminate functions and value properties in objects:

type ObjectFunctions<T> = Pick<T, KeysOfType<T, Function>>
type ObjectProperties<T>= Pick<T, Exclude<keyof T, KeysOfType<T, Function>>>
gcanti commented 6 years ago

Thanks @mattiamanzati