我目前有这种方法
create<T extends ElementType | string>(type: T): Element<T>;
使用
export type ElementType = 'ExtensionElements' | 'Documentation';
export type Element<T> =
T extends 'ExtensionElements' ? ExtensionElements :
T extends 'Documentation' ? Documentation :
GenericElement;
此方法位于.d.ts
中,并且保证始终输入结果,因此
const e1 = obj.create('ExtensionElements');
^^ type is ExtensionElements
const e2 = obj.create('Documentation');
^^ type is Documentation
const e3 = obj.create('Other');
^^ type is GenericElement
现在,我想让此方法的用户 extend 扩展可能的类型选择,例如,
type CustomElementType = 'Other' | ElementType;
type CustomElement<T> =
T extends 'Other' ? CustomOtherElement : Element<T>;
const e4 = obj.create<CustomElementType, CustomElement>('Other');
^^ type is CustomOtherElement
但是,这似乎无法正常工作,因为我总是收到所有类型的联合,并且不能使用任意字符串。
您还有其他想法如何实现吗?
答案 0 :(得分:2)
您可以使用接口将字符串类型映射为真实类型。由于接口是开放式的,因此客户端可以使用模块扩充来添加其他选项:
// create.ts
export declare let obj: {
create<T extends ElementType | string>(type: T): Element<T>;
}
type ExtensionElements = { e: string }
type Documentation = { d: string }
type GenericElement = { g: string }
export type ElementType = 'ExtensionElements' | 'Documentation';
export interface ElementMap {
'ExtensionElements': ExtensionElements;
'Documentation': Documentation;
}
export type Element<T extends string> = ElementMap extends Record<T, infer E> ? E :
GenericElement;
const e1 = obj.create('ExtensionElements'); // ExtensionElements
const e2 = obj.create('Documentation'); // Documentation
const e3 = obj.create('Else'); //GenericElement
// create-usage.ts
import { obj } from './create'
type CustomOtherElement = { x: string }
declare module './create' {
export interface ElementMap {
'Other': CustomOtherElement
}
}
const e4 = obj.create('Other'); // CustomOtherElement
如果要进行作用域扩展,则需要一个额外的函数,该函数将更改用于将字符串映射到对象类型的接口。此方法只可以将当前对象强制转换为预期结果(因为类型在运行时无关紧要,所以不必有所不同)
// create.ts
interface Creator<TMap = ElementMap>{
create<T extends keyof TMap | string>(type: T): Element<TMap, T>;
extend<TMapExt extends TMap>(): Creator<TMapExt>
}
export declare let obj: Creator
type ExtensionElements = { e: string }
type Documentation = { d: string }
type GenericElement = { g: string }
import { obj, ElementMap } from './create'
type CustomOtherElement = { x: string }
export type ElementType = 'ExtensionElements' | 'Documentation';
export interface ElementMap {
'ExtensionElements': ExtensionElements;
'Documentation': Documentation;
}
export type Element<TMap, T extends PropertyKey> = TMap extends Record<T, infer E> ? E : GenericElement;
const e1 = obj.create('ExtensionElements'); // ExtensionElements
const e2 = obj.create('Documentation'); // Documentation
const e3 = obj.create('Else'); //GenericElement
// create-usage.ts
export interface CustomElementMap extends ElementMap {
'Other': CustomOtherElement
}
const customObj = obj.extend<CustomElementMap>()
const e4 = customObj.create('Other'); // CustomOtherElement