OfficeDev / OfficeDev/Office-Addin-Scripts
custom-functions-metadata - Parenthesized types cause type-mismatch.
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 194
- Forks
- 117
- Avg merge
- 1d 32m
- Merged PRs (30d)
- 2
Description
Expected behavior
When using a type in parentheses, the function metadata should be able to be generated correctly. This is specifically needed for arrays.
Current behavior
When I run the metadata tool, it returns an error:
Type doesn't match mappings (6, 23)
Type doesn't match mappings (4,14)
Steps to Reproduce
Create a custom function file with the following definition:
/**
* Do the thing
* @customfunction
* @returns {(string | number)[]} The result
*/
export function foo(): (string | number)[] {
return [];
}
Run the metadata tool against the above file.
However, this also occurs for essentially any type wrapped in parentheses. For example, this code (although silly) also demonstrates the same issue:
/**
* Do the thing
* @customfunction
* @returns {(string)} The result
*/
export function foo(): (string) {
return [];
}
Diagnosis
The specific issue occurs here:
Where the type of the expression for the array is ParenthesizedType. A naive fix would be something like:
node = arrayType.node;
if (node.kind=== ts.SyntaxKind.ParenthesizedType) {
node = node.type;
}
However since it also impacts non array types, perhaps it can try and unwrap the type at the end:
node = unwrapNode(node);
type = TYPE_MAPPINGS[node.kind];
if (!type) {
extra.errors.push(logError("Type doesn't match mappings", typePosition));
}
// This could be extended to account for other weirdness
const unwrapNode = (node: TypeNode): TypeNode => {
let curr = node;
while (curr.kind === ts.SyntaxKind.ParenthesizedType) {
curr = (curr as ParenthesizedTypeNode).type;
}
return curr;
};
Context
- Operating System: Win 11
- Node version: 20.14.0
- Typescript: 5.8.3
- Office version: N/A
- Tool version: 2.1.2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the metadata-tool failure with the custom function examples, then inspect packages/custom-functions-metadata/src/parseTree.ts around line 1311, where ParenthesizedType is reported. The work is done when parenthesized return types, including the array example, generate metadata without type-mismatch errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 58/100