Home>
I want to hear

While migrating code written in JavaScript to TypeScript, I ran into an error like the one below.

  • Error
TS2339: Property'__API__' does not exist on type'Process'.
  • Actual source code
import * as firebase from'firebase/app';
import'firebase/database';
export default function createAPI ({config, version}) {
  let api;
  if (process.__API__) {
    api = process.__API__;
  } else {
    firebase.initializeApp (config);
    api = process.__API__ = firebase.database (). Ref (version);
    api.onServer = true;
  }
  return api;
}

In the process object of Node__API__I know it's an error because it doesn't exist, so
Add a type definition and__API__I'd like to be able to accept, but is there a way to add a type definition to the object provided by Node as standard?

  • Answer # 1

    https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/process.d.ts#L174
    You're probably using the @ types/node type definition,processIs the type ofProcessIs defined here, so I think you can ask for it by extending the interface.

    declare global {
      namespace NodeJS {
        interface Process {
            __API__: any;
        }
      }
    }

    However, I feel that extending global objects is not a good design in the first place. (I was curious, so I looked it up and found an article like this, but I didn't know what this method came from.)
    Normally, you shouldn't define a global object, just define it in a module and import it. Unless there is a clear reason to keep this practice, I would change it. I might cheat on these tricks to get the compilation to work temporarily.