Typescript 错误TS2403:后续变量声明必须具有相同的类型 错误

Typescript 错误TS2403:后续变量声明必须具有相同的类型 错误,typescript,Typescript,我收到以下类型脚本错误: 错误TS2403:后续变量声明必须具有相同的类型。变量“environment”的类型必须为“string”,但此处的类型为“any” 代码 package.json server.ts ./config/config.ts 问题: 要将函数的返回值识别为字符串,我需要做什么 编辑 tsconfig 节点的安装 我添加了一个带有 { "globalDependencies": { "core-js": "re

我收到以下类型脚本错误:

错误TS2403:后续变量声明必须具有相同的类型。变量“environment”的类型必须为“string”,但此处的类型为“any”

代码 package.json server.ts ./config/config.ts 问题: 要将函数的返回值识别为
字符串
,我需要做什么

编辑 tsconfig 节点的安装 我添加了一个带有

{
  "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安装
。我有一个名为
typings
的文件夹,在该文件夹中还有一个名为
node
的文件夹,带有
索引.d.ts
。我假设这与
节点相同。d.ts

编辑2 前端(angularjs)和后端(nodejs)的吞咽
我看到您的文件扩展名是
.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():字符串{
应为:

export = function(): string {
更多

您是否也可以添加tsconfig.json?您是如何安装
node.d.ts
?我无法复制此错误。我使用
typings安装了类型
1.0.4
1.3.1
。使用
typescript@1.8.10
要生成,我没有收到任何错误。VSCode 1.2.1 Windows上也没有错误…您的版本是什么
typings
?尝试更新它可能…@andigga,我猜
config.ts
文件函数可能是一个
胖数组
。而且,
让env:any=process.env.NODE|env| |“development”
谢谢,你的解决方案似乎几乎可以用了。我遇到了两个新问题,1)要么我包括
/config/config.js
,这就是创建d编译后通过gulp,然后我得到一个typescript错误
src/server.ts(5,25):错误TS2307:找不到模块'./config/config.js'.
,因为文件在typescript编译期间不在那里,或者我需要
/config/config.ts
,然后我在运行时得到一个错误。2)我需要切换到
“模块”:“commonjs”
在我的
tsconfig
中,为了避免错误
系统未定义
。现在我的设置主要是从angularjs主页复制的。这是否意味着我需要用两个不同的
tsconfig
文件编译我的打字脚本?我还经常使用
const
,它抱怨重新定义变量,即使
const
在另一个
js
文件中。关于2),这有助于:
gulp.task('scripts-backend',function(){tscConfig.compilerOptions.module='commonjs';…
关于2)最好使用2个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"
  ]
}
{
  "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"
  }
}
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/'));
});
import config = require('./config/config.js'); 
const environment = config();
export = function(): string {