My code consists of a union type called PromptOptions
:
type PromptOptions =
| BasePromptOptions
| BooleanPromptOptions
| StringPromptOptions
type BasePromptOptions = {
kind: string | (() => string)
};
type BooleanPromptOptions = { kind: 'confirm' };
type StringPromptOptions = {
kind: 'input' | 'invisible' | 'list' | 'password' | 'text';
};
What I am attempting to achieve:
I have an arbitrary type named Lookup = { kind: 'invisible' }
, and my goal is to utilize
type ExtractedType<T> = Extract<PromptOptions, T>
so that the outcome becomes ExtractedType<Lookup> = StringPromptOptions
.
This approach successfully works if I provide a type that matches a prompt option exactly (
ExtractedType<{ kind: 'confirm' }> = BooleanPromptOptions
), but when I try something like this: ExtractedType<{ kind: 'invisible' }> = never
, whereas I expect it to be StringPromptOptions
.
Evidently, what I have done so far is incorrect, and I aim to achieve something like
Extract<PromptOptions, T extends <PromptOptions['kind']>>
; however, I am uncertain how to proceed with this or whether it is even feasible.