Skip to content
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

TS7016: Could not find a declaration file for module '@hotwired/turbo-rails' #18

Closed
szTheory opened this issue Dec 23, 2020 · 12 comments
Closed
Assignees

Comments

@szTheory
Copy link

Ran into this error while upgrading a Rails 6 app that uses TypeScript and Turbolinks to the new Turbo. Any ideas? No results on DefinitelyTyped yet.

ERROR in app/javascript/packs/application.ts:10:30
TS7016: Could not find a declaration file for module '@hotwired/turbo-rails'. '/myapp/node_modules/@hotwired/turbo-rails/app/assets/javascripts/turbo.js' implicitly has an 'any' type.
Try `npm i --save-dev @types/hotwired__turbo-rails` if it exists or add a new declaration (.d.ts) file containing `declare module '@hotwired/turbo-rails';`
  8 | 
> 9 | import { Turbo, cable } from "@hotwired/turbo-rails";
@javan
Copy link
Contributor

javan commented Dec 23, 2020

Can you share a sample app or your relevant TypeScript configuration? I can't reproduce in a stock setup on Rails 6.1:

$ rails new wired && \
  cd wired && \
  bin/rails webpacker:install:typescript && \
  yarn add @hotwired/turbo-rails && \
  rm -rf app/javascript/packs/* && \
  echo "import { Turbo, cable } from \"@hotwired/turbo-rails\"" > app/javascript/packs/application.ts && \
  echo "console.log({ Turbo, cable })" >> app/javascript/packs/application.ts && \
  bin/webpack

…

js/application-d58c957e0d7290104479.js.map    204 KiB  application  [emitted] [dev]        application
                             manifest.json  364 bytes               [emitted]              
Entrypoint application = js/application-d58c957e0d7290104479.js js/application-d58c957e0d7290104479.js.map
[./app/javascript/packs/application.ts] 102 bytes {application} [built]

@szTheory
Copy link
Author

Thanks javan. If you add "strict": true to tsconfig.json then run tsc it produces the error. It looks like webpacker transpiles the TypeScript to JS but doesn't run the type checker itself.

@javan
Copy link
Contributor

javan commented Dec 23, 2020

I see. So this is more of a feature request to add TypeScript declarations than an issue since you'd encounter the same problem trying to use any library published without types. Noted! In the meantime, you may find some of these workarounds helpful.

@szTheory
Copy link
Author

Well it used to work in Turbolinks 6 so this is new behavior after upgrading by following the install instructions in the readme but I see your point. Regardless of how you classify it I'm sure a lot of TypeScript users would appreciate type definitions on the interface to help migrate to the new version. Thanks again for looking into this.

@javan
Copy link
Contributor

javan commented Dec 23, 2020

We do have definitions for Turbo itself, which is written in TypeScript, and passes right through this package:

export * as Turbo from "@hotwired/turbo"
It would be pretty straightforward to re-export its types too along with a few manual definitions for:
export async function getConsumer() {
if (consumer) return consumer
const { createConsumer } = await import("@rails/actioncable/src")
return setConsumer(createConsumer())
}
export function setConsumer(newConsumer) {
return consumer = newConsumer
}
export async function subscribeTo(channel, mixin) {
const { subscriptions } = await getConsumer()
return subscriptions.create(channel, mixin)
}

Happy to accept a PR if anyone feels like taking that on!

@Jonnykoder
Copy link

Has anyone solved this issue? I'm having the similar problem on rails 7.

@mockdeep
Copy link

Depending on how deeply you're using Turbo, you can define your own types for your specific use cases. In our case, for the moment, we only disabling Drive, so this worked for us:

// app/javascript/@types/turbo.d.ts
declare module '@hotwired/turbo-rails' {
  type TurboSession = {
    drive: boolean;
  }

  type Turbo = {
    session: TurboSession;
  }

  export const Turbo: Turbo;
}

@ReazerDev
Copy link

Hi, wouldn't it be possible to just release the typings in the npm package?
This should fix this issue :)

@kzkn
Copy link

kzkn commented Oct 25, 2022

I have opened two pullreqs to resolve this issue:

In order to keep up with turbo updates, I thought it would be preferable to convert turbo-rails to TypeScript as well.
If it is too much to convert turbo-rails to TypeScript, I think it would be better to follow the TypeScript community's practice and publish the type information from DT.

What do you think?

If there is a lightweight way to re-export just the type information from turbo-rails without converting to TypeScript, that would be fine too.

@paulorivera27
Copy link

+1 on this one..

@kzkn
Copy link

kzkn commented Jul 29, 2023

I've opened #392 about ten months ago. Please upvote 😄

@brunoprietog
Copy link
Contributor

Turbo left TypeScript. Closing this

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

Successfully merging a pull request may close this issue.

8 participants