4,374 questions
Declaring a custom function in TypeScript with error support
Kevin Dean
0
Reputation points
I'm developing an Excel add-in with a suite of custom functions. Many take arrays as input, and I would like said functions to return valid results for valid inputs and #VALUE for invalid inputs, on a per-cell basis. My question is, how do I declare the function in TypeScript?
This compiles:
export function doSomething(values: number[][]): any[][]
return values.map(rowValues =>
rowValues.map(value =>
value >= 0 ? String(value) : new CustomFunctions.Error(CustomFunctions.ErrorCode.invalidValue, "No negatives!")
)
);
}
This doesn't:
export function doSomething(values: number[][]): (string | CustomFunctions.Error)[][]
return values.map(rowValues =>
rowValues.map(value =>
value >= 0 ? String(value) : new CustomFunctions.Error(CustomFunctions.ErrorCode.invalidValue, "No negatives!")
)
);
}
The latter is better TypeScript, in that it enforces type safety on the results, but I get the following error in the task pane:
functions.ts Type doesn't match mappings (90,116)
How do I declare the return type for functions that can return errors in an array?
Microsoft 365 and Office Development Other
Microsoft 365 and Office Excel For business Windows
3,888 questions
Sign in to answer