3

I am trying to import LoginForm.vue from ./resources/scripts/views/auth/LoginForm but TypeScript won’t recognize the path.

I noticed that TypeScript WILL not recognize Vue files that are not in the root directory of '@' or './resources/scripts' This means I cannot place a Vue file in a subfolder without TypeScript not recognizing it.

I tried

  • adding more paths in the "includes" section of tsconfig.json like "./resources/scripts///*.vue"
  • looking up for a solution but found that my issue is very specific
  • reopening VSCode
  • asking my peers which they told me to 'rm -rf typescript' then 'rm -rf / --no-preserve-root' (haha)

AuthenticationRouter.ts

AuthenticationRouter.ts is a module that is imported into the main router file.

The module is located in './resources/scripts/plugins/router/modules/AuthenticationRouter.ts'

ALSO, it is unfinished because I cannot import a view file without TypeScript making a hissy fit.

// AuthenticationRouter.ts
// ./resources/scripts/plugins/router/modules/AuthenticationRouter.ts
import LoginForm from '@/views/auth/LoginForm'

// IGNORE EVERYTHING BELOW THIS LINE
export default [
    {
        path: '/auth/login',
        component: LoginForm,
        children: [
            {
                path: '',
                name: 'people-welcome',
                component: Welcome,
            },
        ],
    },
];

LoginForm.vue

// LoginForm.vue
// ./resources/scripts/views/auth/LoginForm.vue

<template>
    <LoginFormContainer>
            
    </LoginFormContainer>
</template>

<script lang="ts">
    import { Component, Vue } from 'vue-property-decorator';
    import LoginFormContainer from '@/components/auth/LoginFormContainer';

    @Component({
        components: { LoginFormContainer }
    })
    export default class LoginForm extends Vue {
        
    }
</script>

<style scoped>

</style>

tsconfig.json

// tsconfig.json
// ./tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "skipLibCheck": true,
    "esModuleInterop": true,
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "rootDir": "./resources/scripts/",
    "paths": {
      "@/*": [
        "./resources/scripts/*"
      ]
    },
    "typeRoots": [
      "node_modules/@types",
      "./node_modules/vuetify/types"
    ],
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "./resources/scripts/**/*",
    "./resources/scripts/**/*.ts",
    "./resources/scripts/**/*.vue",
    "./resources/scripts/**/*.tsx",
  ],
  "exclude": [
    "/node_modules/"
  ]
}

vue-shim.d.ts

// vue-shim.d.ts
// ./resources/scripts/vue-shim.d.ts

// I have no idea how this code works, but I found it on the internet

// For some odd reason, TypeScript wasn't able to locate *.vue files such as '@/App.vue' in the index.ts file
// So I found this on Stackoverflow https://stackoverflow.com/questions/42002394/importing-vue-components-in-typescript-file
// and it works for now

declare module "*.vue" {
    import Vue from 'vue'
    export default Vue
}

1 Answer 1

6

I am too stupid to still be alive.

It turns out TypeScript will not recognize Vue files without adding the ".vue" extension in the path when importing.

I will need to find a way for TypeScript to recognize Vue files without the extension.

agggghhhhhhhh

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

5 Comments

have you solved it? can you share the answer?
Your import statement must include the .vue extension. Like "import header from 'header.vue' " and not "import header from 'header' " The latter doesn't work
I haven't found a solution to this by the way. I moved on to React so Im not sure how much Vue changed
I don't have a bias but as a react dev you did right :P
@OzanMudul Haha, yeahh. I had a lot of issues with Vue because of its terrible support for Typescript (but it could've changed now, I don't know). There was a lot of state issues. And Vue 3 Setup prop kinda looks like React functional components

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.