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.

Choices is not a constructor in TypeScript (Vue)

See original GitHub issue

I am receiving this error Error in mounted hook: "TypeError: choices_js_1.default is not a constructor" when trying to instantiate Choices in TypeScript.

Here’s how my component looks like:

<template>

</template>

<script lang="ts">
    import Vue from 'vue';
    import Choices from 'choices.js'
    import {Component, Prop} from 'vue-property-decorator';

    @Component
    export default class Select extends Vue
    {
        @Prop({'default': null})
        public name:string;

        @Prop({'default': () => []})
        public options:Array<any>;

        @Prop({'default': null})
        public selected:any;

        public mounted()
        {
            let choices = new Choices('div');
        }
    }
</script>

Do you have any idea why?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
grzegorztomasiakcommented, Jul 5, 2017

Oh yes!!! Changing module to es6 did the trick and I am able now to make imports like:

import Choices from 'choices.js'

I will google now the differences in module property to get know a bit more what’s happening under the hood.

Thank you very much!

1reaction
grzegorztomasiakcommented, Jul 5, 2017

The whole console log error below:

[Vue warn]: Error in mounted hook: "TypeError: choices_js_1.default is not a constructor"

found in

---> <Select> at /var/www/local.studio/app/Resources/public/app/Common/Components/Select/Select.Component.vue
       <Root>
(unknown) TypeError: choices_js_1.default is not a constructor
    at VueComponent.mounted (eval at <anonymous> (experimental_bundle.js:550), <anonymous>:16:23)
    at callHook (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:2557:21)
    at Object.insert (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:3384:7)
    at invokeInsertHook (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:5212:28)
    at Vue$3.patch [as __patch__] (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:5377:5)
    at Vue$3.Vue._update (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:2317:19)
    at Vue$3.updateComponent (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:2440:10)
    at Watcher.get (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:2779:25)
    at new Watcher (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:2762:12)
    at mountComponent (eval at <anonymous> (experimental_bundle.js:91), <anonymous>:2444:17)

Ok I know the solution:

I had to import Choices like this:

import * as Choices from 'choices.js'

This one fails in my project import Choices from 'choices.js'

I am not sure why, anyway now it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue is not a constructor error, using Typescript & webpack
You want to use this one, since you are using it as an ES module with the import statement. Your alias should be:...
Read more >
TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >
Typescript and Vue in general · Issue #478 · vuejs/vue - GitHub
Pass configuration to instance not via constructor. TypeScript class instances create members after the call to super and thus creates a subtle timing...
Read more >
How To Write Class-Based Components with Vue.js and ...
In this article, you will be introduced to using vue-class-component and vue-property-decorator to support TypeScript in Vue.js class-based ...
Read more >
Polls: Components and Vuex | learn-nuxt-ts
Choice [] ) { this.choices = choices !== undefined ? choices ... For lazy people like me, feel free to add a link...
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