有一个简单的TS包用作CommonJS模块,没有导出.TS文件被编译为具有相同名称的JS文件并用作require('package/option-foo').
tsconfig.json:
{
"compilerOptions": {
"target": "es5"
}
}
Run Code Online (Sandbox Code Playgroud)
期权foo.ts:
declare const GlobalVar: any;
function baz() {}
if (GlobalVar.foo) GlobalVar.baz = baz;
Run Code Online (Sandbox Code Playgroud)
期权bar.ts:
declare const GlobalVar: any;
function baz() {}
if (GlobalVar.bar) GlobalVar.baz = baz;
Run Code Online (Sandbox Code Playgroud)
这里最重要的部分是option-foo与option-bar被从来没有一起使用.项目中还有其他免费的TS文件,但它们不会影响任何东西,只需要在一次tsc运行中转换为JS .
当tsc运行时,它抛出
无法重新声明块范围变量'GlobalVar'.
重复的功能实现.
无法重新声明块范围变量'GlobalVar'.
重复的功能实现.
for GlobalVar和bazin两个文件.
如何在不使构建过程或这两个TS文件的输出复杂化的情况下进行处理?
为了配置我的 React Native 项目,我仔细遵循了以下过程: https: //facebook.github.io/react-native/blog/2018/05/07/using-typescript-with-react-native。但是我要编译这个,我从打字稿编译器中得到了十五个错误。
以下是一些错误:
信息:
"@types/react": "^16.7.3",
"@types/react-native": "^0.57.8",
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "0.49.1",
"react-native-typescript-transformer": "^1.2.10",
"react-test-renderer": "16.6.0-alpha.8af6728",
"typescript": "^3.1.6"
Run Code Online (Sandbox Code Playgroud)
更新
tsconfig.json
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"allowJs": true,
"jsx": "react-native",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"exclude": ["build", "index.js", "node_modules"]
}
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "test",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": …Run Code Online (Sandbox Code Playgroud)