Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wch opened this issue Mar 8, 2025 · 2 comments
Open

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

wch opened this issue Mar 8, 2025 · 2 comments

Comments

@wch
Copy link
Collaborator

wch commented Mar 8, 2025

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://git@github.com/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
Copy link
Collaborator Author

wch commented Mar 8, 2025

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
Copy link
Collaborator Author

wch commented Mar 8, 2025

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:

!window.Shiny ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore; Do not want to define `window.Shiny` as a type to discourage usage of `window.Shiny`;
// Can not expect error when combining with window available Shiny definition
!window.Shiny.shinyapp ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore; Do not want to define `window.Shiny` as a type to discourage usage of `window.Shiny`;
// Can not expect error when combining with window available Shiny definition
!window.Shiny.shinyapp.isConnected()

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy