In my project using Node v14.x and StencilJS (React) v2.3.x, I encountered an issue with a test-helper file containing a function that converts string-arrays to number-arrays:
export function parseNumericStringOrStringArrayToIntegers(value: string | (string | string[])[]): number | (number | number[])[] {
if (typeof value === 'string') {
return parseInt(value);
} else {
return value.map((item) => parseNumericStringOrStringArrayToIntegers(item)) as number | (number | number[])[];
}
}
I decided to move this function to the index.d.ts
of the component for better organization. However, after making this change by replacing export
with declare
and removing unnecessary code, I ran into errors during the npm run build
process:
[ ERROR ] TypeScript: ./src/components/app/index.d.ts:46:108
An implementation cannot be declared in ambient contexts.
L46: declare function parseNumericStringOrStringArrayToIntegers(value: string | (string | string[])[]): ConfigIntValue {
L47: if (typeof value === 'string') {
[ ERROR ] TypeScript: ./src/components/app/index.d.ts:48:5
Statements are not allowed in ambient contexts.
L47: if (typeof value === 'string') {
L48: return parseInt(value);
L49: } else {
This left me puzzled as to how to resolve these errors. It seems like I'm missing something obvious, but what could it be?