Webpack resolve.alias does not work with typescript?

前端 未结 7 1164
日久生厌
日久生厌 2020-12-12 21:02

I try to shorten my imports in typescript

from import {Hello} from \"./components/Hello\";

to import {Hello} from \"Hello\";

<
相关标签:
7条回答
  • 2020-12-12 21:37

    You are missing one very important point in tsconfig.json: The matching pattern!

    It should be configured like this:

    "baseUrl": ".",
    "paths": {
        "appSrc/*": [
            "src/*"
        ]
     }
    

    The "*" is the important part to tell TS to match anything on the right side.

    I found that out from this article: Type-safe es2015 module import path aliasing with Webpack, Typescript and Jest

    NOTE

    • Make sure all your webpack.config.js are updated (e.g. if you use storybook).
    • If you use Visual Studio Code you may need to restart it, in order for the squiggly lines to disappear.
    0 讨论(0)
提交回复
热议问题