How to conditionally set a function argument type in typescript?

I have a generic function I need to call in 2 places

const functionA = (arg1, deleteFunction) => {
 deleteFunction(arg1)
}

when I call it in the two different places I pass in a different deleteFunction each time. these deleteFunctions then update redux but they require different types so I’m getting errors

I was wondering if for arg1 I could specify what type it should be based on properties it contains. something like this

const functionA = (arg1: arg1.hasSomeProperty ? Arg1Types : Arg1OtherType, deleteFunction) => {
 deleteFunction(arg1)
}

obviously this doesn’t work but the 2 deleteFunctions have different types (one has Arg1Typesthe other has Arg1OtherTypes

might be going about it in completely the wrong way. any ideas?

16 thoughts on “How to conditionally set a function argument type in typescript?”

Leave a Comment