简体   繁体   English

TypeScript-如何排除其中一种类型?

[英]TypeScript - how to exclude one of the typings?

I'm creating a web + mobile app using react-native and TypeScript. 我正在使用react-native和TypeScript创建一个Web +移动应用程序。 So essentialy, I have two different outputs for two different TS configs. 非常重要,对于两个不同的TS配置,我有两个不同的输出。

For the web, my target is es5 and module commonjs . 对于网络,我的目标是es5和module commonjs So I have to use the es6-shim typing to make tsc happy when transpiling. 因此,在es6-shim我必须使用es6-shim类型使tsc满意。

For mobile (different tsconfig.json ), my target is es6 and module es6 . 对于移动设备(不同的tsconfig.json ),我的目标是es6和模块es6 The problem is that this collides with the es6-shim typing and tsc outputs many "Duplicate identifier" errors. 问题是这与es6-shim键入冲突,并且tsc输出许多“重复标识符”错误。 If I remove the shim, tsc is happy (but then web transpilation stops working). 如果我删除垫片,则tsc很高兴(但随后Web转换停止工作)。

I've tried excluding the typing in the tsconfig for mobile like so: 我试过排除tsconfig中用于移动设备的键入,例如:

...
"exclude": [
    "typings/globals/es6-shim",
...

...but no luck, TypeScript compiler finds the typing anyway (probably because of a reference from typings/index.d.ts to the es6-shim typing) and still outputs many "Duplicate identifier" errors for the mobile build. ...但是运气不佳,TypeScript编译器仍然找到了键入(可能是由于从typings/index.d.ts到es6-shim键入的引用),并且仍然为移动版本输出许多“重复标识符”错误。

Am I doing the ignore the wrong way? 我是否以错误的方式进行忽略? Am I thinking about it wrong and there is some other way to solve this? 我是否认为这是错误的,还有其他解决方法吗?

If you have two different tsconfig.json files, you could have two typings folders, both with the appropriate files for mobile/web respectively. 如果您有两个不同的tsconfig.json文件,则可以有两个类型的文件夹,两个文件夹分别具有对应于mobile / web的文件。 This would depend slightly on the structure of your project, if there was a separated directory where it would make sense to keep these. 如果存在一个单独的目录来保存这些目录,则这将略微取决于项目的结构。

However, I would probably be inclined to just ignore your index.d.ts file also and explicitly include your declaration files, for each build type. 但是,对于每种构建类型,我可能倾向于只忽略您的index.d.ts文件,并明确包括您的声明文件。 eg 例如

mobile tsconfig.json: 移动tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "noImplicitAny": true,
        "removeComments": true,
    },
    "files": [
        "core.ts",
        "typings/globals/something_else.d.ts"
        // Your other files to be compiled or declarations (everything bar your es6-shim)
    ]
}

and then you could do the same for web, including your es6-shim, or just add everything ie the index.d.ts 然后您可以对网络执行相同的操作,包括es6-shim,或者仅添加所有内容,即index.d.ts

Exclude typings/index.d.ts for the ES6 build as well. typings/index.d.ts包括ES6构建的typings/index.d.ts

If you have something else in that file that needs to be included in the ES6 build, then move out es6-shim reference into a separate .d.ts file for the ES6 build and exclude that instead. 如果该文件中还有其他需要包含在ES6构建中的内容,则将es6-shim参考.d.ts到ES6构建的单独的.d.ts文件中,并将其排除在外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM