question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RequireJS, TypeScript and Popper

See original GitHub issue

CodePen demo

I’ve created a test project to reproduce the issue.

~~https://github.com/chapterjason/proper-issue~~

Steps to reproduce the problem

Im currently starting to use popper.js in my project. The first issue I’ve got that I can’t importing files that ends with .js in require.js (https://github.com/requirejs/requirejs/issues/1560). I could resolving this with adding a mapping instead of a path.

I tried now to import popper with import Popper from 'popper.js'; which ends up in TypeError: popper_js_1.default is not a constructor in the browser. I also tried the other import style import Popper = require('popper.js'); but this ends up in the following semantic errors on the TypeScript compilation:

.....ts(..,..): error TS2694: Namespace ''popper.js'' has no exported member 'PopperOptions'.
.....ts(..,..): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

What is the expected behavior?

After looking in https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.js I expected no semantic errors, cause define(factory) returns the Popper function and not a multiple exports module.

What went wrong?

The index.d.ts the typings went wrong for amd.

Any other comments?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
kadet1090commented, Aug 16, 2018

It’s quite a shame that this issue is not fixed, and yet still occurring because of improper type definitions. My fix consist of “own” typings: https://gist.github.com/kadet1090/b11d4dbe794f6016cb826b516e1fd45b

And then i can require it via:

/// <reference path="./types/popper.js.d.ts" />
import Popper = require("popper.js");

The first line is actually quite important because it tells typescript to use my typings instead of those provided with package.

1reaction
chapterjasoncommented, Aug 29, 2017

I think you misunderstood me. The index.d.ts is incompatible with the umd version.

As you can see there is only one “export

(function(global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
        typeof define === 'function' && define.amd ? define(factory) :
        (global.Popper = factory());
}(this, (function() {
    var Popper = function() {
        [...]
    }();
    return Popper;
})));

module.exports = factory() or define(factory) or factory() will always return the constructor.

In the index.d.ts you’ve got the following.

declare module 'popper.js' {
  export default Popper;
}

And it is not correct in comparsion to the documentation https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-class-d-ts.html

~Also what I found was the following. I saw that Popper use rollup with babel. In this documentation you can see that the result to umd is another as here in Popper. https://babeljs.io/docs/plugins/transform-es2015-modules-umd/~

A closer look I saw that Popper use rollup with output format umd. Line https://github.com/rollup/rollup/blob/152afb9732975188c7f4bf716c143da549afd432/src/finalisers/umd.js#L60 to https://github.com/rollup/rollup/blob/152afb9732975188c7f4bf716c143da549afd432/src/finalisers/umd.js#L64 is the code that will be injected. But it doesnt supports default exports. It seems that is a issue of rollup…

Read more comments on GitHub >

github_iconTop Results From Across the Web

RequireJS, TypeScript and Popper · Issue #405 - GitHub
Im currently starting to use popper.js in my project. The first issue I've got that I can't importing files that ends with .js...
Read more >
Bootstrap 4 error "Bootstrap dropdown require Popper.js", with ...
So, my question: how can I configure Bootstrap 4 with Popper.js in an Aurelia CLI app (using Require.js, not Webpack)?. Thanks. typescript ......
Read more >
[Solved]-Typescript with AMD and require.js-require.js
So after your script has been loaded, you have have a script which should not be in AMD format, it should simply contain...
Read more >
Javascript – Issue Loading PopperJS and Bootstrap via RequireJS ...
bootstrap-4javascriptpopper.jsrequirejstwitter-bootstrap. I'm attempting to load jquery, popperjs, and bootstrap (v4-beta) via requirejs and in the console ...
Read more >
Typings | Popper
Popper comes with type definitions for both Flow and TypeScript. The project is type checked by Flow, and type definitions for TypeScript are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found