Declaring a custom function in TypeScript with error support

Kevin Dean 0 Reputation points
2025-01-08T15:17:26.5666667+00:00

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?

Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
2,072 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,141 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.