Thursday, 27 April 2017

How to set up absolute module import to TypeScript?

What is the "best" way to enable absolute import paths, i.e.

import { my_module } from 'src/my_module_file';

There's conflicting information on this; some claiming that this can be achieved with baseUrl and paths settings in tsconfig.json's compilerOptions, and some people claiming the opposite. As a TypeScript newbie, I'm slightly confused.

Do I need to setup Webpack with resolve.modules setting, or is there a feature in TypeScript compiler for converting absolute paths to relative paths during compilation?

Tried this kind of tsconfig.json without luck:

{
    "compilerOptions": {
        "target": "es2017",
        "module": "commonjs",
        "lib": ["es2017"],
        "removeComments": true,
        "sourceMap": true,
        "baseUrl": "./",
        "paths": {
            "*": ["./*"]
        },
        "rootDir": "server",
        "outDir": "generated",
        "typeRoots": ["node_modules/@types"]
    },
    "include": [
        "server/**/*.ts"
    ],
    "exclude": [
        "node_modules"
    ]
}

ps. using Node.js and running my app with ts-node.



via Kitanotori

No comments:

Post a Comment