typescript - Cannot redeclare block-scoped variable -
i have node program uses commonjs , therefore js files start number of require statements. renamed these js files ts hope incrementally move typescript getting these errors:
from following code:
const rsvp = require('rsvp'); const moment = require('moment'); const firebase = require('universal-firebase'); const email = require('universal-sendgrid'); const sms = require('universal-twilio'); const merge = require('merge'); const typeof = require('type-of'); const promising = require('promising-help'); const _ = require('lodash'); const = require('./upload'); const audience = require('./audiences'); const message = require('./messages');
the locally referenced modules upload
, audiences
, , messages
are define (all?) of same modules such lodash
, etc. i'm guessing somehow namespace scope not being respected between modules i'm not sure why.
i'm unclear whether using es6 import
syntax transpile es5 commonjs "require" module format (this using node 0.10.x).
oh additional context, tsconfig.json
is:
{ "compileroptions": { "target": "es5", "module": "commonjs", "removecomments": true, "sourcemap": true, "outdir": "./dist", "watch": true }, "compileonsave": true }
note: i've seen other people have gotten "cannot redeclare block-scoped variable" error before none of conversations seemed fit situation. although i'm quite new typescript maybe i'm making newbie mistake.
also of note, noticed examples of strange variant of commonjs , ecmascript module formats:
import = require('./upload');
this in contrast how i'd write as:
const = require('./upload');
when use "import" keyword, however, complains upload.ts
not module:
if @ current snapshot of moment.d.ts
on definitelytyped, we'll see global script file (as opposed module) variable named moment
:
declare var moment: moment.momentstatic;
it's considered global script file because doesn't import or export anything.
right your file script file. might not sound correct, idea typescript doesn't recognize require
on own import. require
technically other function.
instead, you'll need use specific syntax this:
import moment = require("moment");
all tell typescript actually import, , safe transform import in commonjs, amd, umd, systemjs etc.
as side note, typescript 2.0, we've updated these .d.ts
files not global script files, , instead authored modules. if see declaration files moment in types-2.0
branch in definitelytyped, can see following lines:
declare var moment: moment.momentstatic; export = moment;
where export = moment
module system-agnostic way of writing module.exports = moment
.
Comments
Post a Comment