34

I want to export several interfaces together with Component from .vue file.

Basic.vue:

<template>
    <div class="app">
        <router-view></router-view>
    </div>
</template>

<script lang="ts">
import { Vue, Component } from "vue-property-decorator";

export interface IBasic {
    name :string;
}

/**
 * Simplest container
 */
@Component
export class Basic extends Vue {}
export default Basic;
</script>

But when I import it from another .ts file, I got:

enter image description here

What can I do to import the interface successfully?

5
  • Why not move IBasic into its own file that could be imported by whatever needs it? Commented May 14, 2019 at 12:46
  • Although it's doable to put them into a separate file, I'm looking for the possibility to put them together with components. Especially when the interfaces are useless elsewhere. Commented May 14, 2019 at 14:38
  • Any news on this issue? Is collecting all exports to one default object only way to go for now? Commented Dec 6, 2019 at 9:37
  • Looks like vscode + [email protected] already fix the problem. Commented Sep 23, 2021 at 2:11
  • I can tell my vue-shim.d.ts file is the issue, but I'm not sure how to fix it Commented Dec 19, 2024 at 23:26

7 Answers 7

2

I had this same problem and realize that despite of no having errors on my IDE, the error was still thrown so I just Restart the server and it works perfectly... I have to do this every time I created a import or export anything with TS.

Sign up to request clarification or add additional context in comments.

1 Comment

This didn't work for me
2

Try moving the interface into another TS file

component.vue

<template>
    <div class="app">
        <router-view></router-view>
    </div>
</template>
<script lang="ts">
import { Vue, Component } from "vue-property-decorator";
   
/**
 * Simplest container
 */
@Component
export class Basic extends Vue {}
export default Basic;
</script>

interfaces.ts

export interface IBasic {
    name :string;
}

Comments

1

You can try to use ktsn/vuetype which will generate a custom declaration file for every component file, instead of using the default shims-vue.d.ts declaration.

1 Comment

This seems like an actual solution unlike the other answers, but it seems a little heavy. Is there a way to modify the default shims-vue.d.ts declaration to accept an arbitrary named export.
1

If you use WS then try to change Vue service in Settings - TypeScript - Vue to Vue Language Serviceenter image description here

Comments

0

Solved this issue in VSCode & vue-class-components by disabling hybrid mode for the vue extension.

{
  "vue.server.hybridMode": false
}

Comments

-1

You can drop the { } from your import. It’s a default export, so then you write import IBasic from "@/../containers/Basic"

2 Comments

There is a default export but there is also a named IBasic which is presumably what is desired.
Why is this upvoted. It doesn't answer the question in any way.
-1

Using this Plugin in VS Code solved the Problem for me: https://github.com/HerringtonDarkholme/vue-ts-plugin

It is a fork that is actively maintained. I ran yarn add -D ts-vue-plugin and added these compiler options to my tsconfig.json:

compilerOptions: {
  "allowSyntheticDefaultImports": true,
  "plugins": [{ "name": "ts-vue-plugin" }]
}

My Code (storybook 6 file button.stories.ts):

import Button, { BUTTON_TYPE } from '@/components/Button.vue';

With this both the type import finally works and I can jump to the file (CMD+click) instead of jumping to the shims-vue.d.ts like it used to. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.