Problem with TS conditional on union

I'm currently playing around with typescript conditionals, and doing conditionals on string literal unions isn't quite working for me
type EventsType = 'ready' | 'data' | 'reset' | 'end'

type Discriminate<T extends EventsType> = T extends 'reset' | 'end'
? () => void
: dosomth<T>

type dosomth<T extends 'data' | 'ready'> = (data: string, type: T) => void
type EventsType = 'ready' | 'data' | 'reset' | 'end'

type Discriminate<T extends EventsType> = T extends 'reset' | 'end'
? () => void
: dosomth<T>

type dosomth<T extends 'data' | 'ready'> = (data: string, type: T) => void
This is the code that i have, but for some reason, TS is complaining that
typescript: Type 'T' does not satisfy the constraint '"data" | "ready"'.
Type 'EventsType' is not assignable to type '"data" | "ready"'.
Type '"reset"' is not assignable to type '"data" | "ready"'. [2344]
typescript: Type 'T' does not satisfy the constraint '"data" | "ready"'.
Type 'EventsType' is not assignable to type '"data" | "ready"'.
Type '"reset"' is not assignable to type '"data" | "ready"'. [2344]
in the dosmth<T> line https://www.typescriptlang.org/play?#code/C4TwDgpgBAogbhAdsAzgFXNAvFA5AJwgEMATEXKAHzxKOCIuoIhQmEbyRNwCgfRIUACIBLFAGN8IgLYjEdCAB40UCAA9gXFLATJ0mAHxQcK9ZsQltzVuyqcLvKFAD8UABQBKY0bgB7ESQ8TgBcUCS+KL7SwAAWygZ8AtDhkdFxphpaNHQMdsyk5EY4brT0oSjAUogA5gA0UEmhaF5YPv6BPEA here is the typescript playground. What am i doing wrong? Why isn't it correctly taking the 'reset' string literal out of T?
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
3 Replies
soydev
soydev11mo ago
Thanks mate
erik.gh
erik.gh11mo ago
sure
Want results from more Discord servers?
Add your server