我认为解释我的情况的最好方法是使用代码:
interface IPluginSpec {
name: string;
state?: any;
}
interface IPluginOpts<PluginSpec extends IPluginSpec> {
name: PluginSpec['name'];
// How to require opts.initialState ONLY when PluginSpec['state'] is defined?
initialState: PluginSpec['state'];
}
function createPlugin<PluginSpec extends IPluginSpec>(
opts: IPluginOpts<PluginSpec>,
) {
console.log('create plugin', opts);
}
interface IPluginOne {
name: 'pluginOne';
// Ideally state would be omitted here, but I can also live with having to
// define "state: undefined" in plugins without state
// state: undefined;
}
// Error: Property 'initialState' is missing in type...
createPlugin<IPluginOne>({
name: 'pluginOne',
// How to make initialState NOT required?
// initialState: undefined,
// How to make any non-undefined initialState invalid?
// initialState: 'anything works here',
});
interface IPluginTwo {
name: 'pluginTwo';
state: number;
}
createPlugin<IPluginTwo>({
name: 'pluginTwo',
initialState: 0,
});
答案 0 :(得分:1)
您可以使用条件类型来执行此操作。有了它,您可以测试该属性是否存在,并且可以拥有或不具有额外的属性:
interface IPluginSpec {
name: string;
state?: any;
}
type IPluginOpts<PluginSpec extends IPluginSpec> = PluginSpec extends Record<'state', infer State> ? {
name: PluginSpec['name'];
initialState: State;
} : {
name: PluginSpec['name']
}
function createPlugin<PluginSpec extends IPluginSpec>(
opts: IPluginOpts<PluginSpec>,
) {
console.log('create plugin', opts);
}
interface IPluginOne {
name: 'pluginOne';
}
// Ok
createPlugin<IPluginOne>({
name: 'pluginOne',
// nothing to add
});
interface IPluginTwo {
name: 'pluginTwo';
state: number;
}
createPlugin<IPluginTwo>({
name: 'pluginTwo',
initialState: 0,
});
对于更可组合的方法,您可以使用具有公共部分的交集,并且每个交集中的可选部分都是自己有条件的:
interface IPluginSpec {
name: string;
state?: any;
config?: any;
}
type IPluginOpts<PluginSpec extends IPluginSpec> = {
name: PluginSpec['name']
}
& (PluginSpec extends Record<'state', infer State> ? { initialState: State; } : {})
& (PluginSpec extends Record<'config', infer Config> ? { initialConfig: Config; } : {})
条件类型对调用者非常有用。问题在于,在实现内部,打字稿无法真正说明条件类型(因为未知T
)。
最好的解决方案是保留公共签名(带有条件类型)和简化的实现签名(没有条件类型)。这样,您就可以在不给类型断言的情况下实现函数,同时为调用者提供所需的行为:
function createPlugin<PluginSpec extends IPluginSpec>(opts: IPluginOpts<PluginSpec>)
function createPlugin<PluginSpec extends IPluginSpec>(opts: {
name: string
initalState: PluginSpec['state'],
initialConfig: PluginSpec['config'],
}) {
if (opts.initalState) {
opts
}
}