I currently have this method
create<T extends ElementType | string>(type: T): Element<T>;
which uses
export type ElementType = 'ExtensionElements' | 'Documentation';
export type Element<T> =
T extends 'ExtensionElements' ? ExtensionElements :
T extends 'Documentation' ? Documentation :
GenericElement;
This method is in a .d.ts, and it guarantees the result is always typed, so that
const e1 = obj.create('ExtensionElements');
^^ type is ExtensionElements
const e2 = obj.create('Documentation');
^^ type is Documentation
const e3 = obj.create('Other');
^^ type is GenericElement
Now, I'd like to let the users of this method extend the possible typed choices, so that, for example
type CustomElementType = 'Other' | ElementType;
type CustomElement<T> =
T extends 'Other' ? CustomOtherElement : Element<T>;
const e4 = obj.create<CustomElementType, CustomElement>('Other');
^^ type is CustomOtherElement
However this doesn't seem to work correctly, as I always receive an union of all types, and I cannot use arbitrary strings.
Do you have any other idea how I could implement this?