#How to extract string union from intersection with object
8 messages · Page 1 of 1 (latest)
Seems difficult. I though this would have the best chance of working, but it doesn't:
type RemoveKind<T> = T extends infer U & { kind: any } ? U : never;
type Test = RemoveKind<"foo" & { kind: "foo"}>
// ^? - type Test = "foo" & {
// kind: "foo";
// }
type Choose<K> = _Choose<Union, K>;
type _Choose<T, K> = T extends infer U & { kind: K } ? U : never;
does this do what you want?
@vernal cliff yes thank you
@vernal cliff Can you explain the dark magic behind infer U & { kind: K } that forces it tosplit away from intersected string?
i'll have to admit that i don't quite know why this works and why Retsam's doesn't. one might expect that TS is doing something along the following lines: given (say) T = DFoo = Foo & { kind: 'foo' }, to infer U such that U & { kind: 'foo' } == DFoo, the natural choice is U = Foo
but of course U = Foo & { kind: 'foo' } is also a valid inference