shiny icon indicating copy to clipboard operation
shiny copied to clipboard

TypeScript can't find `Shiny` in `@types/rstudio-shiny`

Open wch opened this issue 10 months ago • 2 comments

In py-shiny, I locally updated package.json so that instead of pointing to the v1.8.0 tag, it pointed to the types from this repo's v1.1.10 tag:

  "@types/rstudio-shiny": "git+https://[email protected]/rstudio/shiny.git#v1.10.0",

After I did that, TypeScript was no longer able to find the Shiny object:

Image

The problem likely has to do with the globalShiny.d.ts file. This is the 1.10.0 version:

import type { ShinyClass } from "../src/shiny/index";
declare global {
    interface Window {
        Shiny: ShinyClass;
    }
}

This is the 1.8.0 version:

import type { Shiny as RStudioShiny } from "../src/shiny/index";
declare global {
    const Shiny: RStudioShiny;
    interface Window {
        Shiny: RStudioShiny;
    }
    type Shiny = RStudioShiny;
}

Interestingly, TypeScript is able to see it via window.Shiny:

Image

wch avatar Mar 08 '25 03:03 wch

Adding const Shiny: ShinyClass seems to make the errors go away, although I don't know if it will cause other issues.

import type { ShinyClass } from "../src/shiny/index";
declare global {
  const Shiny: ShinyClass;
  interface Window {
    Shiny: ShinyClass;
  }
}

I wonder if it's because there's some configuration difference in the tsconfig between this repo and py-shiny, where py-shiny doesn't know that window is the same global?

@schloerke Do you remember if there was some specific reason not to include const Shiny: ShinyClass;?

wch avatar Mar 08 '25 03:03 wch

Now that I think back on it, I think maybe we were moving toward using window.Shiny. We use that internally in the Shiny source code and in other packages.

For example:

https://github.com/rstudio/shiny/blob/f55c26af4a0493b082d2967aca6d36b90795adf1/srcts/src/shiny/index.ts#L691-L699

https://github.com/rstudio/bslib/blob/b7a02e785a12f2d75d3e12113353d8529019ace2/srcts/src/components/_shinyAddCustomMessageHandlers.ts

That's fine by me -- it's more explicit where Shiny is coming from. According to Claude and ChatGPT, using window.Shiny is the better practice these days. But if we go with this, then we should probably update our docs as well.

We also talk about using window.Shiny in the srcts/README.md file (although in that section we suggest that the developer add a global Shiny to their types): https://github.com/rstudio/shiny/blob/f55c26af4a0493b082d2967aca6d36b90795adf1/srcts/README.md#using-shiny-typescript-definitions

wch avatar Mar 08 '25 15:03 wch