0

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?

  • Is this not ["at least one property is required"](https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist/49725198#49725198)? – kelsny Aug 18 '22 at 03:57
  • @kelly That solves my problem. Thanks. I still wonder if there is another way to do that. – Lorryntz Aug 18 '22 at 04:14
  • [playground link](https://www.typescriptlang.org/play?noImplicitAny=false&target=99&noFallthroughCasesInSwitch=true&allowUmdGlobalAccess=true&pretty=true&ts=4.4.3#code/C4TwDgpgBAChBOBnA9gOygXigbwLACgopUBDAWwgC4pFh4BLVAcwBoCj6ATa1AVzIBGCNoSglOneBESJqtBswIBfAgQD0AKg1R22qACVpvADbAo9RFAgAPSAGNgETlGDIoQ6rqLZi5KjTpGJiglKAAfHHNuYn4heBDwyPFJaVkAhWDQr0SfUgo5QOYWKJ5YhASI3L8CjOLkqRkaoIrIrlLBYTEJBrT5ZqzCPUrffPSg4raYjvg67tSm5gSCDTUCUEgDI1NMKABlXgFECGBEAB44JDQAPlV8deh9w+OzgBUrnZ8AbQBpc3QAawgIGQADMoC8ALqUdiRT4AVT+UG+UPBPwhLS+CMYSJRLzRCQAZHsDkcTqcAPJkejAU4vYrfK43fBKT6A4FgyG3AhAA) – Filly Aug 19 '22 at 17:21
  • @Filly Thanks a lot! That's the way I'm looking for. – Lorryntz Aug 22 '22 at 08:35
  • How does it work? First we are creating an object with every like ```types Subsets = { [K in keyof T] : T[K]} ``` so nothing special is happening here The next step is to instantly access all values of the object to recieve an union type of all values ```types Subsets = { [K in keyof T]:T[K]}[keyof T] => string | number``` By creating an inner object we create a union type of every key value pair ```types Subsets = { [K in keyof T]: { [U in K]: T[K] } }[keyof T] => { name: string } | { id: number } | { address: string }``` – Filly Aug 22 '22 at 13:11
  • By omiting every key and in the recursive call we create all subsets of the key value pair – Filly Aug 22 '22 at 13:12
  • [link](https://www.typescriptlang.org/play?noImplicitAny=false&target=99&noFallthroughCasesInSwitch=true&allowUmdGlobalAccess=true&pretty=true&ts=4.7.4#code/C4TwDgpgBAChBOBnA9gOygXigbwLACgopUBDAWwgC4pFh4BLVAcwBoCj6ATa1AVzIBGCNvgIBfAgQD0AKhlR28qACUIiXgBtgUeoigQAHpADGwCJyjBkUIdUVFsxclRp1GTKGKgAfHDu7E-ELwnj5+JJyc8GqI1LQMzKH2YY6kFHFuzCz+PEEIob6pzhkJrFARUTEl7gV+XLmCwuWR0YixrqVJhEqFTukd7tn1gY3w2RWt7fE1XgQyUgSgkCpqmtpYAMq8AogQwIgAPHBIaAB8kvhL0Fs7e4cAKqeYUADaANYQIMgAZlD3ALr6AxmVCcPQvVAQABuCEBAH4cF5qI4XgBpHToD5fX4AyjsPwvACqGKgqP+1HuaMBXl6RJJZIpVNCADIoDddvsDgB5Mj0YAHe7ZVGnc74MTvT4-P7-C4EIA) – Filly Sep 01 '22 at 11:01
  • I found a bug in my implementation so you should use the link in the previous comment to prevent this bug – Filly Sep 01 '22 at 11:02

0 Answers0