Skip to content

Instantly share code, notes, and snippets.

@alexsasharegan
Created July 18, 2019 23:38
Show Gist options
  • Save alexsasharegan/810e6e551853a1e43c5d3afcf26409db to your computer and use it in GitHub Desktop.
Save alexsasharegan/810e6e551853a1e43c5d3afcf26409db to your computer and use it in GitHub Desktop.
/**
* Creates a mapped type from the `Subject`
* that excludes all members with types
* that extend the `ExcludeType`.
*/
type ExcludeBy<Subject, ExcludeType> = {
[P in {
[K in keyof Subject]: Subject[K] extends ExcludeType ? never : K
}[keyof Subject]]: Subject[P]
};
/**
* Creates a mapped type from the `Subject`
* that includes only members with types
* that extend the `IncludeType`.
*/
type IncludeOnly<Subject, IncludeType> = {
[P in {
[K in keyof Subject]: Subject[K] extends IncludeType ? K : never
}[keyof Subject]]: Subject[P]
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment