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

[Typescript] Cannot call a namespace #62

Open
MTCoster opened this issue Nov 9, 2018 · 3 comments
Open

[Typescript] Cannot call a namespace #62

MTCoster opened this issue Nov 9, 2018 · 3 comments

Comments

@MTCoster
Copy link

MTCoster commented Nov 9, 2018

According to the typings from qrcode.d.ts, I should be able to do the following since the export is an instance of the QRCodeFactory interface which has the call signature (typeNumber: TypeNumber, errorCorrectionLevel: ErrorCorrectionLevel) : QRCode;:

import * as qrGenerator from 'qrcode-generator';

function myFunc() {
  const qr = qrGenerator(0, 'H');
  qr.addData('Hello, world!');
  qr.make();
}

However, the typescript compiler returns:

Error: Cannot call a namespace ('qrGenerator')

Alternatively, using qrcode like this compiles without a hitch:

import * as qrGenerator from 'qrcode-generator';

function myFunc() {
  const qr = qrcode(0, 'H');
  qr.addData('Hello, world!');
  qr.make();
}

But it throws a runtime error:

Uncaught ReferenceError: qrcode is not defined
@MTCoster
Copy link
Author

MTCoster commented Nov 9, 2018

Update: I've found a workaround, but it's not nice.

import * as qrGenerator from 'qrcode-generator';

function myFunc() {
  // @ts-ignore
  const qr = qrGenerator.default(0, 'H');
  qr.addData('Hello, world!');
  qr.make();
}

@joseph-navant
Copy link

What about this?

import qrcode from 'qrcode-generator';

componentDidLoad() {
  const typeNumber = 4 as TypeNumber;
  const errorCorrectionLevel = 'L' as ErrorCorrectionLevel;
  const qr = qrcode(typeNumber, errorCorrectionLevel);
  qr.addData('Hi!');
  qr.make();
  document.getElementById('placeHolder').innerHTML = qr.createImgTag();
}

@sezginruhi
Copy link

Update: I've found a workaround, but it's not nice.

import * as qrGenerator from 'qrcode-generator';

function myFunc() {
  // @ts-ignore
  const qr = qrGenerator.default(0, 'H');
  qr.addData('Hello, world!');
  qr.make();
}

in the file "... node_modules/qrcode-generator/qrcode.d.ts" add

default

interface QRCodeFactory {
  default (qrVersion: number, errorCorrectionLevel: string): QRCode;
  stringToBytes(s: string) : number[];
  stringToBytesFuncs : { [encoding : string] : (s: string) => number[] };
  createStringToBytes(unicodeData: string, numChars: number) :
  (s : string) => number[];
}

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