DEV Community

Discussion on: Aha! Understanding Typescript's Type Predicates

Collapse
 
beraliv profile image
beraliv

Would be good to cover if we can create Predicate type which will infer type based on some value in it:

type A = { a: 1 } | { a: 2; b: 2 } | { a: 3; b: 3; c: 3 }

// expect { one: { a: 1 }; two: { a: 2; b: 2 }; three: { a: 3; b: 3; c: 3 } };
type B = {
  [K in A['a']]:  // implementation
  // infer { a: 1 } if K equals 1
  // infer { a: 2; b: 2 } if K equals 2
  // infer { a: 3; b: 3; c: 3 } if K equals 3
  // But with using only A, not repeating the part of it
}
Enter fullscreen mode Exit fullscreen mode

Maybe sounds like another article