I have encountered these errors due to the presence of the strictNullChecks
parameter, which is activated by the strict
configuration in the tsconfig.json
file. It appears that when arguments.length === 2
, the ab
function should be available (thanks to Function Overloading). So, why does the error persist? Are there any suggestions for enhancing the code or resolving this issue?
15 errors found within the switch
TS2722: Unable to call an object that could potentially be undefined
my tsconfig.json
{
"compilerOptions": {
"module": "CommonJS",
"noUncheckedIndexedAccess": true,
"strict": true,
"target": "ESNext"
}
}
function Σ<A>(a: A): A;
function Σ<A, B>(a: A, ab: (a: A) => B): B;
function Σ<A, B, C>(a: A, ab: (a: A) => B, bc: (b: B) => C): C;
function Σ<A, B, C, D>(a: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D): D;
function Σ<A, B, C, D, E>(a: A, ab: (a: A) => B, bc: (b: B) => C, cd: (c: C) => D, de: (d: D) => E): E;
function Σ<A, B, C, D, E, F>(
a: A,
ab: (a: A) => B,
bc: (b: B) => C,
cd: (c: C) => D,
de: (d: D) => E,
ef: (e: E) => F
): F;
function Σ(a: unknown, ab?: Function, bc?: Function, cd?: Function, de?: Function, ef?: Function): unknown {
switch (arguments.length) {
case 1:
return a;
case 2:
return ab(a);
case 3:
return bc(ab(a));
case 4:
return cd(bc(ab(a)));
case 5:
return de(cd(bc(ab(a))));
case 6:
return ef(de(cd(bc(ab(a))));
}
}