Eric Wang
Eric Wang

Reputation: 436

TypeScript unable to locate module (vue file) when trying to import

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

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
}

Upvotes: 3

Views: 1958

Answers (1)

Eric Wang
Eric Wang

Reputation: 436

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

Upvotes: 6

Related Questions