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

Cannot Import in TypeScript #335

Closed
jamie-thul opened this issue Feb 5, 2018 · 25 comments
Closed

Cannot Import in TypeScript #335

jamie-thul opened this issue Feb 5, 2018 · 25 comments

Comments

@jamie-thul
Copy link

According to the description, I should be able to import this module into TypeScript as follows:
import * as SignaturePad from 'signature_pad';

TypeScript refuses to recognize this as valid because there isn't an exported module. Please export a module for TypeScript compatibility.

Here is a stack overflow topic regarding this.

@szimek
Copy link
Owner

szimek commented Feb 5, 2018

If it doesn't work, you can try to import directly from signature_pad/dist/signature_pad.mjs.

The next version will most likely be rewritten in TypeScript - see typescript branch. I "just" need to find some free time to clean it up and figure out rollup config for all versions (umd es5, es6, non-minified, minified etc.) and how to generate TS declaration files.

@strongui
Copy link

strongui commented Feb 6, 2018

@szimek Can con firm that this still does not work inside a .tsx file.

import * as SignaturePad from 'signature_pad'; Gives SignaturePad is not a constructor

import * as SignaturePad from 'signature_pad/dist/signature_pad.mjs'; Gives SignaturePad is not a constructor

const SignaturePad = require('signature_pad'); Gives SignaturePad is not a constructor

import SignaturePad from 'signature_pad'; Works, but gives a typescript error in the console: ...node_modules/@types/signature_pad/index.d.ts' is not a module.

@szimek
Copy link
Owner

szimek commented Feb 6, 2018

What exactly is in SignaturePad variable in this case? Code in signature_pad/dist/signature_pad.mjs is simply bundled together ES6 source code with a default export. It should just work, unless TS doesn't handle default exports.

@strongui
Copy link

strongui commented Feb 6, 2018

@szimek TS handles default exports just fine, the problem is in the @types/signature_pad index.d.ts definitions.

@szimek
Copy link
Owner

szimek commented Feb 6, 2018

But this library does not provide TypeScript declaration files.

@strongui
Copy link

strongui commented Feb 6, 2018

@szimek it doesn't? Then what is installed when you do install @types/signature_pad --save-dev, there is definitely TS declaration files. Are those not made the same people??

@szimek
Copy link
Owner

szimek commented Feb 6, 2018

I don't know ;) You can see all files being published e.g. here: https://www.jsdelivr.com/package/npm/signature_pad.

@strongui
Copy link

strongui commented Feb 6, 2018

@szimek I just noticed my comment before didn't show the whole install line, because of the @ character. I've edited it. Basically if you do install @types/signature_pad --save-dev, you will see the definitions installed in the @types/signature_pad folder. I wonder who's doing that? Maybe we need to contact them and fix it and merge it into the default package lol

@MrJoe
Copy link

MrJoe commented Feb 9, 2018

Looks like you're using a definition file from DefinitelyTyped .

If I read the declaration correctly this might work (sorry didn't try it out, I just stumbled upon this issue):
import { SignaturePad } from 'signature_pad';

@Selman-EE
Copy link

Selman-EE commented Feb 13, 2018

@strongui I got the same mistake as you. I tried everything in the comments,
I also tried your comments @szimek . Absolutely not worked.
I using the signature pad in the JS file at now but I have to change place to TS
I using node version v6.11.2 and Typescript version is v2.6.2
npm version { npm: '3.10.10', ares: '1.10.1-DEV', node: '6.11.2', zlib: '1.2.11' }

@szimek
Copy link
Owner

szimek commented Feb 18, 2018

I've just tried it out using TypeScript 2.7.2 and it works fine.

index.ts

import SignaturePad from 'signature_pad';

new SignaturePad(document.querySelector('canvas'));

index.html

<html>
  <body>  
    <canvas style="border: 2px solid #000"></canvas>
    <script src="bundle.js"></script>
  </body>
</html>

tsconfig.json

{
  "compilerOptions": {
    "outDir": ".",
    "noImplicitAny": true,
    "module": "es6",
    "target": "es3"
  }
}

webpack.config.js

module.exports = {
  entry: './index.js',  
  output: { filename: 'bundle.js' },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [ '.ts', '.js' ]
  }
};

Run yarn add signature_pad typescript webpack ts-loader && yarn run webpack. Then open index.html and it should work.

@Selman-EE
Copy link

Selman-EE commented Feb 23, 2018

@szimek thanks it works for me

@pablomaurer
Copy link

pablomaurer commented Feb 26, 2018

Works for me using TypeScript 2.4.2 and importing it like @szimek in the comment above.

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

@onurkose
Copy link

onurkose commented Mar 2, 2018

For those are getting SignaturePad is not a constructor error under the hood of requirejs and webpack (or laravel mix) just use with default, as:
new SignaturePad.default(canvas, { backgroundColor: 'rgb(255, 255, 255)' });

@szimek
Copy link
Owner

szimek commented Mar 13, 2018

I just released a beta version that is rewritten in TypeScript. Declaration files are now provided by the library. You can install it using yarn add signature_pad@beta. The source code is still available only on typescript branch.

It still uses the default export, but I tested it with Webpack 4 and TypeScript 2.7.2 and it works if I import it using import SignaturePad from 'signature_pad'.

It would be awesome to get some feedback, if it works, if there are any issues etc.

@StevenBarnettST
Copy link

We're using the beta version in a TypeScript 2.8 app and it works fine.

Just minor issues:

  • Getting an unhandled error on touch end because const touch = event.targetTouches[0] is undefined.
  • The toDataURL method should have its arguments marked as optional if you want to match canvas's toDataURL method.

@szimek
Copy link
Owner

szimek commented Apr 12, 2018

@StevenBarnettST Fixed! Thanks for reporting these issues. Tomorrow I'll release a new beta version.

@Ross-Rawlins
Copy link

What is the correct way to bind the TYPE/interface of signaturepad to a variable, for autocomplete and ts checking?

@Flinsch
Copy link

Flinsch commented Mar 24, 2019

For those are getting SignaturePad is not a constructor error under the hood of requirejs and webpack (or laravel mix) just use with default, as:
new SignaturePad.default(canvas, { backgroundColor: 'rgb(255, 255, 255)' });

This works for me. Thanks a lot! (Symfony Webpack Encore)

@sharabiania
Copy link

I did a workaround to get it to work:
var SignaturePad = require('signature_pad/dist/signature_pad.js');

@UziTech UziTech closed this as completed Jun 18, 2021
@StephanBijzitter
Copy link

StephanBijzitter commented Jun 27, 2021

esModuleInterop: true is what triggers this issue; without it: the library imports correctly.

For example, this is how I have to import it: const {default: SignaturePad} = require('signature_pad');

@RishabSwift
Copy link

This is how I fixed it (in Webpack/Mix)

let SignaturePad = require('signature_pad');
window.SignaturePad = SignaturePad.default;

@nushankodikara
Copy link

nushankodikara commented Aug 25, 2021

Found a way to make it work but It's not for everyone. I managed it to work using the most basic JS and HTML

  1. import from CDN to HTML page ( Using version 1.3.5 )

<script src="https://cdnjs.cloudflare.com/ajax/libs/signature_pad/1.3.5/signature_pad.min.js" integrity="sha512-kw/nRM/BMR2XGArXnOoxKOO5VBHLdITAW00aG8qK4zBzcLVZ4nzg7/oYCaoiwc8U9zrnsO9UHqpyljJ8+iqYiQ==" crossorigin="anonymous" referrerpolicy="no-referrer" ></script>

  1. setup the canvas as usual

<canvas id="signature-pad" class="signature-pad" width="400" height="200" style=" width: 400px; height: 200px; border-radius: 1em; border-style: dashed; border-width: 1px; " ></canvas> <button type="button" id="clearsig" class="btn btn-danger"> Clear </button>

  1. add the functional script to HTML Page

<script> let canvas = document.getElementById("signature-pad") let signaturePad = new SignaturePad(canvas, { backgroundColor: "rgb(250,250,250)", penColor: "rgb(0, 0, 0)", }); document.getElementById("clearsig").addEventListener("click", function () { signaturePad.clear() }); </script>

  1. retrieve data from JS file

let data = signaturePad.toDataURL("image/png")

It will work, But it's not for everyone.

Screenshot (19)

@danielausparis
Copy link

For posterity.
Frankly, this module stuff is laughable. It requires in-depth study of the module framework, next you have to use huge frameworks such as npm or yarn, and lastly, after hours of precious time invested, it does'nt work (see above dialogs). This is ridiculous. Remember, this library is just 11k !!
I thank dearly nushankodikara for his comment that allowed me to use the pre-module, pre-TS, pre-obfuscated version 1.3.5 and just write a simple <script src="jslibs/signature_pad.min.js"></script> and that's it. Just works !
This module stuff is typically a case of project governance gone nuts. Trying to solve a non-existent problem with huge tools and abstraction layers so complex that nobody (see dialogs above) is anymore able to have even the slightest idea of what is going on.

@minimul
Copy link

minimul commented Jun 16, 2023

With importmaps setup e.g.
pin 'signature_pad', to: 'https://cdn.jsdelivr.net/npm/signature_pad@4.0.0/dist/signature_pad.umd.min.js'

I'm just doing import 'signature_pad' but also because none of the more orthodox import declarations worked.

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

No branches or pull requests