【问题标题】:Optional argument effects return type可选参数影响返回类型
【发布时间】: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


    【解决方案1】:

    我相信这行得通:

    export type SingleArgFunc = (arg: any) => any
    export type SingleArgFuncs = { [k: string]: SingleArgFunc }
    
    type CallbackAvailable<CbExt, Cb, IfA, NotA> = Cb extends SingleArgFunc ? CbExt extends ReturnType<Cb> ? IfA : NotA : never
    
    type Bextender<A> = (a: any) => any
    type Cextender<A> = any
    type Dextender<A, B, C> = CallbackAvailable<Bextender<A>, B, null, true>
    
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 2022-01-23
      • 2017-06-01
      • 2022-11-05
      相关资源
      最近更新 更多