Typescript 如何为使用ts loader的样式化组件获取打字?
我正在使用ts loader和webpack来编译我的typescript文件。我已经能够让我的设置在@types模块中的所有模块中工作,但是我不能让它与提供自己的打字文件的样式化组件一起工作 webpack.config.js:Typescript 如何为使用ts loader的样式化组件获取打字?,typescript,webpack,styled-components,ts-loader,Typescript,Webpack,Styled Components,Ts Loader,我正在使用ts loader和webpack来编译我的typescript文件。我已经能够让我的设置在@types模块中的所有模块中工作,但是我不能让它与提供自己的打字文件的样式化组件一起工作 webpack.config.js: module.exports = function(env) { return { module: { rules: [ { test: /\.jsx?$/,
module.exports = function(env) {
return {
module: {
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
babelrc: false,
cacheDirectory: true,
}
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
configFile: path.resolve(__dirname, "tsconfig.json")
},
}
]
},
resolve: {
modules: ['node_modules'],
extensions: ['.js', '.jsx', '.ts', '.tsx']
},
context: __dirname,
target: 'web',
};
};
tsconfig.json:
{
"compilerOptions": {
"outDir": "./dist",
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"moduleResolution": "node",
"jsx": "react",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
}
}
当我尝试从“样式化组件”导入{ThemedStyledFunction}时
我得到:
我尝试通过添加以下内容手动导入样式化组件的键入:
"files": [
"node_modules/styled-components/typings/styled-components.d.ts"
],
但这似乎不起作用。我甚至尝试在@types下创建一个名为styled components的目录,并将打字复制到index.d.ts,但我仍然遇到同样的错误。看起来Webpack可能错误地解释了
styled components的包.json
,并在错误的位置查找打字。提示:如果运行webpack--verbose
,是否会得到显示webpack正在查看哪些文件的输出?下面是--verbose[108]节点_modules/styled components/dist/styled-components.browser.es.js 84.6kb{36}[depth 1][builded]的相关输出[导出:css、关键帧、injectGlobal、isStyledComponent、ConsolidaTestStreamedStyles、ThemeProvider、withTheme、ServerStyleSheet、StyleSheetManager、\u不使用\u或\u您将\u被幽灵缠住\u默认]有趣的是,它在导出中没有显示主题样式功能。我不知道为什么。这些似乎与打字定义描述的内容也不匹配。对,主题样式功能是一个接口,所以它不存在于JavaScript中。Webpack加载JavaScript是有意义的。问题是为什么Webpack不存在o加载TypeScript声明文件。应该会有一些额外的输出显示package.json文件的处理。您能发布它吗(可能是在长度限制较高的地方)?我从未使用过Webpack,但我可能可以从输出中对此进行研究。我没有看到任何类似的东西。我看到的只是构建样式化组件的那一行,然后是导入样式化组件的一堆行。整个输出只是一个入口点列表,然后是一个完全类似的行列表。
"files": [
"node_modules/styled-components/typings/styled-components.d.ts"
],