0

Node v14.x, StencilJS (React) v2.3.x

I had a test-helper file with an exported 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 that this function should be in index.d.ts of the component, so I could use it more logically in the component itself. I moved it and changed export to declare, removed import {parseNumericStringOrStringArrayToIntegers} from "./index"; where it was altered by JetBrains IDE > move, and strangely expected stuff to 'just work'. It didn't...

It threw me two errors in npm run build that seem relevant, but I have no clue how to fix it:

[ 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 {

Apparently, I'm missing something obvious here, but what?

sneeuwitje
  • 131
  • 1
  • 1
  • 6
  • https://stackoverflow.com/questions/26946495/what-means-ambient-in-typescript holds the answer to **why**, but I'm still in the blank on **how to fix it**. – sneeuwitje Nov 03 '21 at 01:37

1 Answers1

1

I think the issue here may be that you're placing the implementation in index.d.ts:

I decided that this function should be in index.d.ts of the component

Files that end with .d.ts are type declaration files, which are special files in TypeScript that cannot contain implementations of functions, only their type information. Without seeing the entire codebase, I suspect that this is the cause. If you move the code to index.ts or any other non-.d.ts, this may resolve your issue

Ryan Waskiewicz
  • 319
  • 4
  • 11