How can I reference typescript files without absolute paths?

前端 未结 2 2030
遥遥无期
遥遥无期 2021-02-18 17:31

Since typescript doesn\'t seem to support absolute path references, I can\'t see how to keep my references tidy. I\'ve got ts files at many different locations in my folder stru

2条回答
  •  深忆病人
    2021-02-18 18:21

    In order to keep your relative path references tidy, specify path alias(es) in your tsconfig.json

    First of all install tspath an npm tool for resolving ts paths NOTE: tspath requires a very recent versison of node!

    npm install -g tspath
    

    Next, add path aliases to tsconfig.json

    "baseUrl": "./",
      "paths": {
        "@Scripts/*": ["./Scripts/typings/jquery/*"],
        "@Plugins/*": ["./MyPlugins/V2/*"]
      }
    

    Now use your alias when referencing your scripts

    @Scripts/jquery
    @Plugins/myPlugin
    

    After you have run the TypeScript compiler, from your project directory, run:

    tspath
    

    or

    tspath -f
    

    to skip the prompt!

    Read more at: https://www.npmjs.com/package/tspath

    Hope this is what you asked for!

提交回复
热议问题