Andi Giga
Andi Giga

Reputation: 4182

error TS2403: Subsequent variable declarations must have the same type

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

Upvotes: 39

Views: 58439

Answers (5)

NotX
NotX

Reputation: 2415

In my case, @types/node was pointing to the wrong version: (node@17 while was still using @node@16). In general, I'ld not rely on commands like --skipLibCheck - they will just hide the underlying issue. Did it take me an unreasonable amount of time (removing all dependencies and re-adding them step by step) to find the issue? Yes! Would I do it again? Yes!

Upvotes: 2

Matthieu Riegler
Matthieu Riegler

Reputation: 55816

In my case @types/node was not a direct dependency but a nested one mostly, reference by wildcard versions "@types/node": "*".

Since I didn't want add it in my direct dependencies, I updated the pinned version in the package-lock by running npm update @types/node.

Upvotes: 1

Try adding --skipLibCheck flag to the tsc script

Ex: tsc --skipLibCheck

Upvotes: 16

TOPKAT
TOPKAT

Reputation: 8688

In my case it was @types/node that was triggering the error on AbortSignal type declaration.

  • In some of my projects, a fresh (re)install of @types/node to latest version fixed the bug

  • In some others, I have uninstalled @types/node which was probably duplicated somewhere. Doing so didn't change anything at my dev workflow (not sure why I installed it in the first place :-/)

Upvotes: 59

basarat
basarat

Reputation: 276293

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

Upvotes: 4

Related Questions