I have the following file to declare a global variable

globals.d.ts

import { Game } from './Game'; declare const game: Game; export {};

However when I call the variable in a file I get the warning

Card.ts

class Card { ... draw() { // Cannot find name `game` game.hoverContext = { id: this.id, type: 'card', }; } }

How am I meant to detect it in my TypeScript app? Is there a tsconfig property I should be adjusting?

jonrsharpe's user avatar

jonrsharpe

124k31 gold badges280 silver badges489 bronze badges

pythonNovice's user avatar

3

UPDATE: Based on feedback from @VLAZ, I updated my main.ts to export my game instance and import that to my other modules. This works and is more concise than my previous attempt

main.ts

import Game from 'Game'; export const game : Game = new Game()

and now in each of my modules, I'm declaring game

module.ts

import { game } from '../main'; class Card { ... draw() { -- no errors since it has type declaration in main.ts -- game.hoverContext = { id: this.id, type: 'card', }; } } }

pythonNovice's user avatar

2 Comments

If you're already importing it, then why bother making it global? It's literally more code than if you just kept to the imports.

2026-02-06T17:02:44.057Z+00:00

Ah I think I understand now, you're saying I should be creating an instance of Game and just export that? I think I fixed that now so I'll update my answer

2026-02-06T18:14:40.11Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.