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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38209699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:37:30  来源:igfitidea点击:

error TS2403: Subsequent variable declarations must have the same type

typescript

提问by Andi Giga

Error

错误

I'm getting this typescript error:

我收到此打字稿错误:

error TS2403: Subsequent variable declarations must have the same type. Variable 'environment' must be of type 'string', but here has type 'any'.

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

Code

代码

package.json

包.json

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

server.ts

服务器.ts

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

./config/config.ts

./config/config.ts

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

Question:

问题:

What do I need to do, to get the return value of the function recognized as a string?

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

Edit

编辑

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"
  ]
}

Installation of node

安装节点

I added a typings.jsonwith

我加了一个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"
  }
}

And as I remember uses tsd install. I have a folder called typings, In that folder is another folder called nodewith an index.d.ts. I assume this is the same as a node.d.ts?

我记得使用tsd install. 我有一个文件夹,名为typings,在该文件夹被称为另一个文件夹nodeindex.d.ts。我认为这与 a 相同node.d.ts

Edit 2

编辑 2

gulp for frontend (angularjs) and backend (nodejs)

前端(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/'));
});

回答by basarat

I see that your file extensions are .ts(e.g. config.ts). Don't use var/requirein ts files! This is especially true for bringing in other tsfiles.

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

var environment = require('./config/config.js')()should be:

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

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

And module.exports = function(): string {should be :

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

export = function(): string {

More

更多的

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

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