【问题标题】:error TS2403: Subsequent variable declarations must have the same type错误 TS2403:后续变量声明必须具有相同的类型
【发布时间】:2016-11-07 15:51:13
【问题描述】:

错误

我收到此打字稿错误:

错误 TS2403:后续变量声明必须具有相同的类型。变量'environment'必须是'string'类型,但这里有'any'类型。

代码

package.json

...
"typescript": "^1.8.10",
...

server.ts

var environment = require('./config/config.js')()

./config/config.ts

module.exports = function(): string {
  //Environment
  let env:string = process.env.NODE_ENV || 'development'
  return env
}

问题:

我需要做什么,才能获得识别为string的函数的返回值?

编辑

tsconfig

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

节点的安装

我添加了一个typings.json

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160602141332",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#6.0.0+20160621231320"
  }
}

我记得使用tsd install。我有一个名为typings 的文件夹,在该文件夹中是另一个名为node 的文件夹,带有index.d.ts。我认为这与node.d.ts?

编辑 2

前端 (angularjs) 和后端 (nodejs) 的 gulp

const gulp = require('gulp');
const gutil = require('gulp-util');
const paths = gulp.paths;
var $ = require('gulp-load-plugins')();

const tscConfig = require('../tsconfig.json');

gulp.task('scripts-frontend', function () {
  gulp.src(paths.src + '/systemjs.config.js')
   .pipe(gulp.dest(paths.out + '/'));

  return gulp.src([paths.src + '/frontend/**/*.ts', paths.typings + '/**/*', '!' + paths.src + '/**/*.spec.ts'])
    .pipe($.sourcemaps.init())
    .pipe($.typescript(tscConfig.compilerOptions))
    .pipe($.sourcemaps.write('.'))
    .pipe(gulp.dest(paths.out + '/frontend/'));
});

gulp.task('scripts-backend', function () {
  return gulp.src([paths.src + '/backend/**/*.ts', paths.typings + '/**/*', paths.src + '/server.ts', '!' + paths.src + '/**/*.spec.ts'])
    .pipe($.sourcemaps.init())
    .pipe($.typescript(tscConfig.compilerOptions))
    .pipe($.sourcemaps.write('.'))
    .pipe(gulp.dest(paths.out + '/backend/'));
});

【问题讨论】:

  • 你能添加你的 tsconfig.json 吗?你是怎么安装node.d.ts的?
  • 我无法复制此错误。我用typings 1.0.41.3.1 安装了这些类型。使用typescript@1.8.10 构建我没有错误。在 VSCode 1.2.1 Windows 上也没有错误...您的 typings 版本是什么?尝试更新它也许......
  • @AndiGiga,我猜config.ts 文件函数可能是fat arraow。还有,let env:any = process.env.NODE_ENV || 'development'

标签: typescript


【解决方案1】:

我看到您的文件扩展名是.ts(例如config.ts)。不要在 ts 文件中使用 var/require !对于引入其他 ts 文件尤其如此。

var environment = require('./config/config.js')() 应该是:

import config = require('./config/config.js'); 
const environment = config();

module.exports = function(): string { 应该是:

export = function(): string {

更多

https://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html

【讨论】:

  • 谢谢,您的解决方案似乎几乎可以工作。我遇到了两个新问题,1)要么我包含 ./config/config.js,它是在编译后通过 gulp 创建的,然后我得到一个打字稿错误 src/server.ts(5,25): error TS2307: Cannot find module './config/config.js'.,因为在打字稿编译期间文件不存在,或者我需要 ./config/config.ts 然后我运行时出错。
  • 2) 我需要在我的tsconfig 中切换到"module":"commonjs" 以避免出现错误System is not defined。现在我的设置主要是从 angularjs 主页复制的。这是否意味着我需要用两个不同的tsconfig 文件编译我的打字稿?
  • 我也经常使用const,它抱怨重新定义变量,即使const 在另一个js 文件中。
  • 关于 2),这有帮助:gulp.task('scripts-backend', function () { tscConfig.compilerOptions.module = 'commonjs';...
  • 关于 2) 最好使用 2 个 tsconfigs。在 linting 错误时,编辑器通常会选择最接近的那个。
猜你喜欢
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 2017-11-15
  • 2018-04-18
  • 1970-01-01
  • 2020-01-03
  • 2017-07-14
相关资源
最近更新 更多