【发布时间】:2019-11-23 22:56:13
【问题描述】:
下面我试图有一个函数,它接受一个可选参数B 并有条件地处理返回,如果传入与否。
export type SingleArgFunc = (arg: any) => any
export type SingleArgFuncs = { [k: string]: SingleArgFunc }
type Bextender<A> = (a: any) => any
type Cextender<A> = any
type Dextender<A, B, C> = B
// type Dextender<A, B, C> = B extends ????? : null : boolean
function Example<A extends SingleArgFuncs, B extends Bextender<A>>(a: A, b?: B) {
return function Inner<C extends Cextender<B>>(c: C): Dextender<A, B, C> {
return {} as Dextender<A, B, C>
}
}
const returnIs = Example({}) // const returnIs: <C extends any>(c: C) => Bextender<{}>
const returnIs2 = Example({}, () => 'meow') // const returnIs2: <C extends any>(c: C) => () => string
这似乎可以通过检查 B 的返回类型来工作,但由于某种原因,条件使它成为一个交集boolean | null。
export type SingleArgFunc = (arg: any) => any
export type SingleArgFuncs = { [k: string]: SingleArgFunc }
type Bextender<A> = (a: any) => any
type Cextender<A> = any
type Dextender<A, B, C> = B extends SingleArgFunc ? ReturnType<B> extends Bextender<A> ? null : boolean : never
function Example<A extends SingleArgFuncs, B extends Bextender<A>>(a: A, b?: B) {
return function Inner<C extends Cextender<B>>(c: C): Dextender<A, B, C> {
return {} as Dextender<A, B, C>
}
}
const returnIs = Example({}) // const returnIs: <C extends any>(c: C) => boolean | null
const returnIs2 = Example({}, () => 'meow') // const returnIs2: <C extends any>(c: C) => boolean
如果B 已定义或未定义,我如何编写条件返回。
【问题讨论】:
标签: typescript