I need to test each value of multiple HTMLInputElements using a/some test function(s), and auto-generate a nested list of checkboxes for each, with the conditions which are expressed as Conditions below:
type TestFunction = (value: string) => boolean
type TestFunctionObject = { [description: string]: Conditions }
type Conditions = TestFunction | TestFunctionObject
So, for example, if I have:
const conds1: Conditions = {
"Consists of alphanumerics": /^[a-zA-Z0-9]$/.test
}
I get a checkbox labeled as "Consists of alphanumerics". And if:
const conds2: Conditions = /^[a-zA-Z0-9]$/.test
I don't want a checkbox but just validate with that.
Auto-generating is done without any problem. Then, I wrote a type which represents validity of each TestFunction:
type Validity<C extends Conditions> = C extends TestFunction
? boolean
: { [K in keyof C]: Validity<C[K]> }
Now I got an error from TS on C[K]; playground here. It says Type 'Conditions[K]' is not assignable to type 'TestFunctionObject'. Type conditioning doesn't seem to narrow Conditions to just TestFunctionObject.
How can I get it to work?
Addition for jcalz's answer: Playground with examples