I am using the intl-ts library. The following function does not compile because it complains that lang[result] is not known to be executable:
function convertResult<
T extends Messages &
{ [P in K]: (fieldName: string, p1: P1, p2: P2, p3: P3, p4: P4) => string },
K extends keyof T,
P1 = any,
P2 = any,
P3 = any,
P4 = any
>(
result: K | null,
params: [P1, P2, P3, P4],
lang?: Intl<T>,
fieldName?: string
): boolean | string | null {
if (result === null) {
return lang ? null : true
} else {
if (lang) {
return lang[result](fieldName, ...params)
} else {
return false
}
}
}
I believed that the definition of K (type of result) would be enough for the compiler to know that lang[result] is indeed a method which takes the appropriate parameters.
So, I wonder if the problem is:
- something I missed in this code.
- something I missed in intl-ts (I am the maintainer of this package).
- or something too complex for Typescript and maybe I should open an issue for it.
A simple example is provided here
Messages &is the part that is causing problems. What does that interface contain ?