// To start with, I aim to adjust the selectivity of the current function parameter based on a specific generic parameter type mentioned previously. My idea involves utilizing parameter sets and tuples.
// However, implementing parameter sets to dynamically set optional parameters leads to various abnormalities.
// For the sake of simplicity, I have created a basic example below:
interface Options<I> {
init?: I,
getInit?: () => I
}
function fun1 <P>(par: P) {
function fun2 <I, O extends P extends boolean ? [options: Options<I>] : [options?: Options<I>]>(...arg: O) {
return {
p: '' as P,
i: '' as I
}
}
return fun2
}
// My initial concern: Generic type I remains unknown.
// Another issue arises with the lack of mutual constraint between the type of init and the return type of getInit. This appears to be connected to the first problem.
const { p, i } = fun1(true)({
init: "1",
getInit: () => "1"
})
How can I address these abnormalities, or are there alternative methods to dynamically set optional function parameters?
For the second revision:
After some experimentation, I have devised certain solutions to the existing issues. However, potential side effects are yet to be determined. I am documenting these for reference.
The provided example lacked the utilization of the generic parameter O. The subsequent example incorporates O and accurately determines its input content.
function fun1<P>(par: P) {
function fun2<I, O extends Options<I>>(
...arg: P extends boolean
? [options: Options<I> & O]
: [options?: Options<I> & O]
) {
return {
p: '' as P,
i: '' as I,
g: '' as unknown as O extends { getInit: () => any } ? true : false
}
}
return fun2
}
const { p, i, g } = fun1(true)({
init: "1",
getInit: () => "1"
})
// p boolean
// i string
// g true