what am i trying to do is, i have a function and I'm passing a bunch of boolean flags to it, each of these flags requires different arguments to be passed to same function.
So here is a simple version:
enum FLAGS {
A = 'a',
B = 'b',
C = 'c'
}
interface ARelatedArgs {
aBool: boolean
aString: string
}
interface BRelatedArgs {
(bEvt: any): void
bBool: boolean
}
interface CRelatedArgs {
(cEvt: any): string
cString: string
}
interface MyFunctionArgs {
flags: Partial<Record<FLAGS, boolean>>
// other props based on flag(s) // i can supply one or more flags here!
}
function myFunction(args: MyFunctionArgs) {
// do something
}
now i want to have type inference based on these call types:
// 1st Call to my function
myFunction({
flags: { [FLAGS.A]: true }
// all ARelatedArgs
})
// 2nd Call to my function
myFunction({
flags: { [FLAGS.A]: true, [FLAGS.B]: true }
// all ARelatedArgs
// + all BRelatedArgs
})
// last Call to my function
myFunction({
flags: { [FLAGS.A]: true, [FLAGS.B]: true, [FLAGS.C]: true }
// all ARelatedArgs
// + all BRelatedArgs
// + all CRelatedArgs
})
basically what i want is to check argument types/infer them based on flags that is been passed to it. The interesting thing is that, I dont want to make checks only based on one flag, I could have passed all my 3 flags (A, B, C) and IntelliSense may help me with that? is it possible to have this behavior in typescript?
I know I'm demanding runtime checking from typescript but any idea how can i implement such a thing?
basically i want MyFunctionArgs
to be something like this:
interface MyFunctionArgs {
flags: Partial<Record<FLAGS, boolean>>
...(FLAGS.A in flags && ARelatedArgs),
...(FLAGS.B in flags && BRelatedArgs),
...(FLAGS.C in flags && CRelatedArgs),
}
Thanks in advance