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'.
...
"typescript": "^1.8.10",
...
var environment = require('./config/config.js')()
module.exports = function(): string {
//Environment
let env:string = process.env.NODE_ENV || 'development'
return env
}
What do I need to do, to get the return value of the function recognized as a string
?
{
"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"
]
}
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
?
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/'));
});
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 {
https://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With