相关疑难解决方法(0)

'无法在不相关的文件中重新声明块范围变量'

有一个简单的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-foooption-bar从来没有一起使用.项目中还有其他免费的TS文件,但它们不会影响任何东西,只需要在一次tsc运行中转换为JS .

tsc运行时,它抛出

无法重新声明块范围变量'GlobalVar'.

重复的功能实现.

无法重新声明块范围变量'GlobalVar'.

重复的功能实现.

for GlobalVarbazin两个文件.

如何在不使构建过程或这两个TS文件的输出复杂化的情况下进行处理?

typescript tsc

52
推荐指数
3
解决办法
2万
查看次数

使用 TypeScript 配置 React Native 时出错

为了配置我的 React Native 项目,我仔细遵循了以下过程: https: //facebook.github.io/react-native/blog/2018/05/07/using-typescript-with-react-native。但是我要编译这个,我从打字稿编译器中得到了十五个错误。

以下是一些错误:

  • 无法重新声明块作用域变量“navigator”。
  • 后续的属性声明必须具有相同的类型。属性“geolocation”必须为“Geolocation”类型,但此处的类型为“GeolocationStatic”
  • 找不到名称“地图”。
  • 后续变量声明必须具有相同的类型
  • 重复的标识符“RequestInfo”。
  • 'FormData' 也在这里声明。
  • 无法重新声明块作用域变量“console”。
  • 不重新声明块作用域变量“navigator”。

信息:

"@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)

node.js npm typescript reactjs react-native

3
推荐指数
1
解决办法
5554
查看次数

标签 统计

typescript ×2

node.js ×1

npm ×1

react-native ×1

reactjs ×1

tsc ×1