假设我有一些JSON文件(让我们将其命名为template.json)
{
"myField1": "",
"myField2": ""
}我也有一个泛型类
export default GenericClass<T> {
// Creating an empty constuctor with passed type.
// to allow define type automatically.
// This allow us not to manually set generic type for class
// and also allows Webpack to pick up changes.
constructor(template?: T) {}
// ...some fields and methods
get typedField(): T {
return /* something slightly calculated */
}
}在我的打字稿项目中,我把它当作一种类型:
import GenericClass from "path/to/GenericClass"
import template from "template.json"
export type TemplateType = typeof template
export default new GenericClass(template)
// we can also write
// export default new GenericClass<TemplateType>()
// but in this case the changes in template.json
// won't be picked up by Webpack.
// However, this does not affects the problem,
// it occurs in both cases.我正在运行webpack开发服务器,并在以下地方使用它:
import * as React from "react"
import GenericInstance from "path/to/GenericInstance"
export default MyComponent extends React.Component {
render() {
var { typedField } = GenericInstance
return (
<main>
<p>{typedField.myField1} {/* ok */}</p>
<p>{typedField.myField2} {/* ok */}</p>
</main>
)
}
}之后,我将在我的template.json中添加一个新字段
{
"myField1": "",
"myField2": "",
"myField3": ""
}保存它。webpack开发服务器在template.json中获取这一更改。好的。--的一件重要事情是,VSCode的自动完成工作(它在可用字段列表中显示了这个myField3 )。很好。
此时,当我试图在MyComponent中使用MyComponent(如<p>{typedField.myField3}</p>)时,awesome-typescript-loader在编译期间发送一个错误:
属性'myField3‘在类型'{“myField1: string;"myField2":string;}’上不存在
显然,awesome-typescript-loader没有看到template.json中的更改,后者是作为MyGenericClass的类型使用的。
我怎么能打败它?在重新启动开发服务器之后,它可以正常工作,直到我在template.json中进行更改为止。
部分webpack.config.js、package.json和tsconfig.json
config = {
rules: {
{
test: /\.tsx?$/,
loader: "awesome-typescript-loader",
exclude: /node_modules/
},
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
},
}
}{
"devDependencies": {
"awesome-typescript-loader": "^5.2.1",
"source-map-loader": "^0.2.4",
"typescript": "^3.3.3",
"webpack": "^4.29.3",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.1.14"
}
}{
"compilerOptions": {
"module": "esnext",
"target": "es5",
"moduleResolution": "node",
"baseUrl": "src",
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"strict": false,
"sourceMap": true,
"outDir": "dist/",
"jsx": "react",
"traceResolution": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowJs": true,
"declaration": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"types": [ "node" ],
"lib": ["es6", "dom", "dom.iterable"],
"downlevelIteration": true,
"resolveJsonModule": true,
"typeRoots": [
"./node_modules/@types"
]
},
"include": [
"src/**/*"
]
}更新
我可以确认,这种情况只发生在导入的*.json中。可能,问题可能与TypeScript的resolveJsonModule设置有关,但不确定。在useCache中为awesome-typescript-loader显式地将false和usePrecompiledFiles设置为false没有帮助。我的意思是,改变了的webpack.config.js现在看起来像是:
{
test: /\.(t|j)sx?$/,
loader: "awesome-typescript-loader",
options: {
useCache: false,
usePrecompiledFiles: false
},
exclude: /node_modules\/(?!superagent)/,
},发布于 2019-04-01 14:49:23
这是awesome-typescript-loader中的一个bug。在5.2.1版本中,下面是一个快速解决方案:
// node_modules/awesome-typescript-loader/dist/instance.js
// ln: 214:
- var EXTENSIONS = /\.tsx?$|\.jsx?$/;
+ var EXTENSIONS = /\.tsx?$|\.jsx?|\.json$/;显然,作者忘记将.json扩展作为一个有效的目标。
https://stackoverflow.com/questions/54695319
复制相似问题