#Limit generic type to only callback functions with parameters

3 messages · Page 1 of 1 (latest)

static crater
#

Hi,

Consider the follow code:

type AsyncFunction<Param, Result> = (p: Param) => Promise<Result>

function createAsyncWithCallback<Param, Result>(fn: AsyncFunction<Param, Result>) {
    return fn
}

function fnWithParams(params: string) {
    return Promise.resolve(params)
}

function fnWithoutParams() {
    return Promise.resolve(null)
}

const fn1 = createAsyncWithCallback(fnWithParams);
const fn2 = createAsyncWithCallback(fnWithoutParams);

fn1("param")
fn2("any-param")

I would have thought that creating fn2 shouldn't work because the callback does not have any params and I have defined my generic type to only allow callbacks with params.

Any ideas? -- thanks

stoic quarry
static crater
#

I am still wrapping my understanding around this -- yes it is not possible 🙂