How to make tsconfig work with webpack provide plugin?

Viewed 1030

I have simple component which use styled-components and my utils.tsx file which contains theme colors. I load it by WebpackProvidePlugin because I do not want to include React and styled-components modules in every component file. It works, renders, compiling fine, but I have some TS errors in console and editor because typescript compiler/parser does not recognize webpack aliases

TS2686: 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.

TS2304: Cannot find name 'Styled'.

TS2304: Cannot find name 'Color'.

Navigation.tsx

const Nav = Styled.nav`
    height: 64px;
    background-color: ${Color.darkBlue};
`

function Navigation() {
    return <Nav>dwa</Nav>
}

export default Navigation

webpack.config.js

...
        new webpack.ProvidePlugin({
            React: 'react',
            Device: [
                path.resolve(path.join(__dirname, 'src/utils/utils.tsx')),
                'devices',
            ],
            Color: [
                path.resolve(path.join(__dirname, 'src/utils/utils.tsx')),
                'colors',
            ],
            Styled: ['styled-components', 'default'],
        }),
...

tsconfig.json

{
    "compilerOptions": {
        "module": "none",
        "moduleResolution": "node",
        "jsx": "react",
        "target": "es6",
        "lib": ["es2015", "dom", "dom.iterable"],
        "sourceMap": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "plugins": [{"name": "typescript-tslint-plugin"}],
        "baseUrl": "./",
        "paths": {
            "components/*": ["src/components/*"],
            "utils/*": ["src/utils/*"]
        }
    },
    "include": ["src/**/*.tsx"],
    "exclude": ["node_modues"]
}
1 Answers

One way about it is to go into your tsconfig.json and set:

"allowUmdGlobalAccess": true
Related