Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2403: Subsequent variable declarations must have the same type

Tags:

typescript

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'.

Code

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
}

Question:

What do I need to do, to get the return value of the function recognized as 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.json with

{
  "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 node with an index.d.ts. I assume this is the same as a node.d.ts?

Edit 2

gulp for frontend (angularjs) and backend (nodejs)

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/'));
});
like image 953
Andi Giga Avatar asked Oct 31 '22 00:10

Andi Giga


1 Answers

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

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

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

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

export = function(): string {

More

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

like image 186
basarat Avatar answered Dec 14 '22 23:12

basarat