I have the following type with 2 generic parameters:
type Result<INPUT, SPEC> = ...
The concrete type of Result depends on the various combinations of INPUT and SPEC.
How it should work:
if(INPUT extends {[key: string]: any}) {
if(SPEC extends {[key: string]: any}) {
Result = {[key in keyof INPUT]: SPEC[key] !== undefined
? Result<INPUT[key], SPEC[key]>
: true
}
} else {
// ...
}
} else {
// ...
}
In words:
- If
INPUTandSPECare objects, thenResultshould be an object with the keys ofINPUT. - For every key of
INPUT: Check ifSPECcontains the same key - If
true, then the value for this key isResult<INPUT[key], SPEC[key]> - If
false, then the value for this key istrue
Additional Information:
- If
INPUTandSPECare objects, thenSPECis a subset ofINPUT. This means:INPUTcan be{ foo: 42, bar: 42 }, butSPECis allowed to only containfooorbaror both or be empty.
Is this somehow possible with TypeScript?