首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >令人敬畏的-类型记录-加载器不会获取JSON中的更改。

令人敬畏的-类型记录-加载器不会获取JSON中的更改。
EN

Stack Overflow用户
提问于 2019-02-14 16:49:21
回答 1查看 1.6K关注 0票数 8

假设我有一些JSON文件(让我们将其命名为template.json)

代码语言:javascript
复制
{
    "myField1": "",
    "myField2": ""
}

我也有一个泛型类

代码语言:javascript
复制
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 */
    }
}

在我的打字稿项目中,我把它当作一种类型:

代码语言:javascript
复制
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开发服务器,并在以下地方使用它:

代码语言:javascript
复制
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中添加一个新字段

代码语言:javascript
复制
{
    "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.jspackage.jsontsconfig.json

代码语言:javascript
复制
config = {
    rules: {
        {
            test: /\.tsx?$/,
            loader: "awesome-typescript-loader",
            exclude: /node_modules/
        },
        {
            enforce: "pre",
            test: /\.js$/,
            loader: "source-map-loader"
        },
    }
}
代码语言:javascript
复制
{
    "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"
    }
}
代码语言:javascript
复制
{
    "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显式地将falseusePrecompiledFiles设置为false没有帮助。我的意思是,改变了的webpack.config.js现在看起来像是:

代码语言:javascript
复制
{
    test: /\.(t|j)sx?$/,
    loader: "awesome-typescript-loader",
    options: {
        useCache: false,
        usePrecompiledFiles: false
    },
    exclude: /node_modules\/(?!superagent)/,
},
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-01 14:49:23

这是awesome-typescript-loader中的一个bug。在5.2.1版本中,下面是一个快速解决方案:

代码语言:javascript
复制
// node_modules/awesome-typescript-loader/dist/instance.js

// ln: 214:
- var EXTENSIONS = /\.tsx?$|\.jsx?$/;
+ var EXTENSIONS = /\.tsx?$|\.jsx?|\.json$/;

显然,作者忘记将.json扩展作为一个有效的目标。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54695319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档