【问题标题】:Best practice to setup tsconfig "files" vs "include"?设置 tsconfig“文件”与“包含”的最佳实践?
【发布时间】:2019-01-19 23:57:26
【问题描述】:

我想知道在 tsconfig 中使用“文件”与“包含”有什么好处和优缺点?

我不太喜欢包含模式,因为它只是将所有 ts 文件包含在 src 文件夹中,我现在可能想要它。

我喜欢“文件”的方法,因为我可以指向入口文件并加载文件需要的所有内容。

我正在使用带有 webpack 的打字稿。我猜入口点是在 webpack 中定义的,所以也不需要在 typescript 中定义?

我尝试使用“文件”,但似乎无法设置文件夹来查找自定义类型定义:typescript with tsconfig with "files" => import image module not found

【问题讨论】:

    标签: typescript tsconfig


    【解决方案1】:

    TypeScript 官网上有tsconfig.jsontwo examples,一个带有"files" 属性,另一个带有"include""exclude" 属性:

    使用"files" 属性

    {
        "compilerOptions": {
            // irrelevant
        },
        "files": [
            "core.ts",
            "sys.ts",
            "types.ts",
            "scanner.ts",
            "parser.ts",
            "utilities.ts",
            "binder.ts",
            "checker.ts",
            "emitter.ts",
            "program.ts",
            "commandLineParser.ts",
            "tsc.ts",
            "diagnosticInformationMap.generated.ts"
        ]
    }
    

    使用"include""exclude" 属性

    {
        "compilerOptions": {
            // irrelevant
        },
        "include": [
            "src/**/*"
        ],
        "exclude": [
            "node_modules",
            "**/*.spec.ts"
        ]
    }
    

    因此,基本上,"files" 用于通过路径直接指定单独的文件,而 "include""exclude" 用于定位文件或文件夹的集合或组等。

    【讨论】:

    • 可以同时使用吗?
    • @Marek:是的——“文件”中的东西永远不会被“排除”模式排除,如果你添加任何东西,而“包含”中的东西会。 (如果您只添加一个“files”数组并期望您之前拥有的所有其他内容保持原样,那么还可以添加默认规则"include": ["**/*"],当您没有“files”规则时您可以免费获得该规则。)
    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多