ThomasReggi
ThomasReggi

Reputation: 59525

Conditional Optional Argument Type

Currently I have this example which demonstrates conditional arguments, The value of the second argument passed in will depend on what the type of the first is.

type Check<G, T> = T extends number ? string : number

function Example<T>(arg: T) { 
    return function <P>(arg: Check<P, T>) { 

    }
}

// Valid:

Example('hello')(1)
Example(1)('hello')

How can I change the code above to make this example work?

Example()(1)
Example(1)()

The issue when I add ? it make's it permanently optional, and without it its required.

Upvotes: 2

Views: 79

Answers (1)

ThomasReggi
ThomasReggi

Reputation: 59525

void does the trick!

type Check<G, T> = T extends number ? void : number

function Example<T>(arg?: T) { 
    return function <P>(arg: Check<P, T>) { 

    }
}

Example()(1)
Example(1)()

Upvotes: 2

Related Questions