0
votes

I have a variable defined like this:

(global as any).State = {
  variables: {},
};

My question is, how do I declare the type signature of State? If I say (global as any).State: Something = ..., the compiler gives me an error saying ; expected.

As far as I can tell, it's the same question as this one, but it's about the window variable, not the global variable: How do you explicitly set a new property on `window` in TypeScript?

1
I think, ; expected has rather something to do with JS ASI. Try to insert a semicolon before above code snippet and it also should work (though without strong types).ford04

1 Answers

1
votes

global is the Node.JS equivalent of window and a global variable declaration contained in @types/node:

// @types/node/globals.d.ts
declare var global: NodeJS.Global;
global
export {}

declare global { // declare global is TS specific, it is not the Node global!
  namespace NodeJS {
    interface Global {
      State: {
        variables: {}
      }
    }
  }
}
globalexportimport
declare namespace NodeJS {
  interface Global {
    State: {
      variables: {}
    }
  }
}

You will then be able to set State without relying on any:

global.State = { variables: {} }