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

Vue is undefined when importing in Typescript #7655

Closed
leebenson opened this issue Feb 15, 2018 · 3 comments
Closed

Vue is undefined when importing in Typescript #7655

leebenson opened this issue Feb 15, 2018 · 3 comments

Comments

@leebenson
Copy link

leebenson commented Feb 15, 2018

Version

2.5.13

Reproduction link

https://codesandbox.io/s/4895o6nm24

Steps to reproduce

Using Typescript 2.7.1, importing Vue 2.5.13 results in undefined.

To reproduce, create a blank .ts file:

import Vue from "vue";
console.log(Vue); // <-- will be `undefined` (or `null` in Code Sandbox)

This can be fixed by importing like:

import * as Vue from "vue";

... but then new Vue() returns the TS error:

test.ts (3,1): Cannot use 'new' with an expression whose type lacks a call or construct signature. (2351)

Changing this to new Vue.default(); resolves the TS error, but then results in a Node error:

TypeError: Vue.default is not a constructor

What is expected?

No errors

What is actually happening?

See above

Note: I'm not using Webpack in this example. What I assume is happening (referencing the recommended tsconfig.json config) is that import statements are being as-is by Typescript, for Webpack to pick up and use properly.

However, I think that this should be improved to work with Typescript directly, without requiring Webpack to enter the mix. This would benefit server-only environments where code transpilation isn't necessarily required.

@ktsn
Copy link
Member

ktsn commented Feb 16, 2018

Duplicate of #7392
You may want to enable --esModuleInterop https://www.typescriptlang.org/docs/handbook/compiler-options.html

@ktsn ktsn closed this as completed Feb 16, 2018
@terrymun
Copy link

terrymun commented Apr 5, 2018

@leebenson Just want to say, thanks for the revelation! I have consistently experienced issues with webpack + TypeScript not resolving imports properly, and it turns out that using import * as Vue from "vue"; works, while import Vue from "vue" actually returns null: https://codesandbox.io/s/mjvjw2xw39

@Kocal
Copy link

Kocal commented Jul 14, 2018

Thank you so much @ktsn, it works like a charm ❤️ ✨

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

5 participants