A type Subsets<T>
is what I need: it generates a union of all non-empty subsets of type T
and it differs from the built-in Partial<T>
in that type {}
doesn't extend Subsets<T>
.
For example:
type Person = {
name: string,
id: number,
address: string
}
/**
* Result is expected to be:
* { name: string } | { id: number } | { address: string }
* | { name: string, id: number } | { name: string, address: string } | { id: number, address: string }
* | { name: string, id: number, address: string }
*/
type Result = Subsets<Person>
How to implement Subset<T>
in TypeScript? Is it feasible to code that in a way like how we solve this kind of problem in algorithm challenges(see this: https://leetcode.com/problems/subsets/), by using DFS and backtracking or something else?