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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

rough.js and Typescript #145

Closed
puigfp opened this issue Apr 19, 2020 · 9 comments
Closed

rough.js and Typescript #145

puigfp opened this issue Apr 19, 2020 · 9 comments

Comments

@puigfp
Copy link

puigfp commented Apr 19, 2020

Hi!

First of all, thanks for creating this package 馃槃

I had some trouble getting rough.js to work on a create-react-app / Typescript setup.

When importing rough.js using import rough from "roughjs/bundled/rough.esm.js", I got this error:

Could not find a declaration file for module 'roughjs/bundled/rough.esm.js'. '/Users/francisco/dev/puzzle-game/node_modules/roughjs/bundled/rough.esm.js' implicitly has an 'any' type.
Try npm install @types/roughjs if it exists or add a new declaration (.d.ts) file containing declare module 'roughjs/bundled/rough.esm.js'; TS7016

I'm a beginner with TypeScript, so I'm not sure what would be the best solution to fix that, but it seems that adding the following file solves the issue.

roughjs/bundled/rough.esm.d.ts

declare module "roughjs/bundled/rough.esm.js" {
  import rough from "roughjs/bin/rough";
  export default rough;
}

I'm assuming similar files could be created for the other bundles as well.

Would you be open to a PR for this change ? or do you think there is a better way of solving the issue?

@pshihn
Copy link
Collaborator

pshihn commented Apr 19, 2020

The types field is set in the package.json file.

Can you share a sample project setup where you are experiencing this? Thanks

@puigfp
Copy link
Author

puigfp commented Apr 20, 2020

Here is a sample project setup: https://github.com/puigfp/roughjs-types-issue

I created it using npx create-react-app roughjs-types-issue --template typescript and then added roughjs to the dependencies.

I can see the types field in the package.json, maybe it's taken into account only when importing using import rough from "roughjs";, not using import rough from "roughjs/bundled/rough.esm.js" ?

@pshihn
Copy link
Collaborator

pshihn commented Apr 20, 2020

Perhaps. It may depend on the tsconfig set up for you by create-react-app.
Here's a vanilla project: https://github.com/pshihn/roughjs-typescript-example

You may be right as it depends on how you import it. Is
import rough from "roughjs" not working for you? Because the module file is correctly set in package.json.

I will try your sample repository later today.

@puigfp
Copy link
Author

puigfp commented Apr 20, 2020

When using import rough from "roughjs";, the types get detected correctly by my editor (VS Code), but I get this error when I start a development server with npm run start:

TypeError: roughjs__WEBPACK_IMPORTED_MODULE_3___default.a.svg is not a function

@pshihn
Copy link
Collaborator

pshihn commented Apr 20, 2020

Argh it always comes down to Webpack! haha
Okay, I will take a look later today.

@puigfp
Copy link
Author

puigfp commented Apr 20, 2020

Thanks !

@pshihn
Copy link
Collaborator

pshihn commented Apr 21, 2020

I looked at your project, and this should work:

import rough from "roughjs/bin/rough";

@puigfp
Copy link
Author

puigfp commented Apr 21, 2020

Yes indeed it works. Thank you.

@puigfp puigfp closed this as completed Apr 21, 2020
@steveruizok
Copy link

I looked at your project, and this should work:

import rough from "roughjs/bin/rough";

Would love to see this in the docs!

DylanSp pushed a commit to DylanSp/tic-tac-toe-react that referenced this issue Aug 13, 2022
* React 16 -> React 18 (upgrade typing, switch to new root API)
* Cypress upgraded to 8.x (will do upgrades with major breaking changes later)
* Modify roughjs imports - rough-stuff/rough#145 (comment)
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

3 participants