Matthew Harwood Matthew Harwood - 5 months ago 60
TypeScript Question

Cannot redeclare block-scoped variable 'reducer' (typescript)

I'm trying to run some sample code from a book and I'm getting an error

Cannot redeclare block-scoped variable 'reducer'
from the
and I'm not sure why?

tsc -v
// Version 2.1.0-dev.20160726

Then I run ts-node like so:

ts-node 01-identity-reducer.ts


interface Action {
type: string;
payload?: any;

interface Reducer<T> {
(state: T, action: Action): T;

let reducer: Reducer<number> = (state: number, action: Action) => {
return state;

console.log( reducer(0, null) ); // should output -> 0


TSError: тип Unable to compile TypeScript
01-identity-reducer.ts (10,5): Cannot redeclare block-scoped variable 'reducer'. (2451)
at getOutput (/Users/person/.nvm/versions/node/v5.0.0/lib/node_modules/ts-node/src/index.ts:258:17)
at /Users/person/.nvm/versions/node/v5.0.0/lib/node_modules/ts-node/src/index.ts:267:16
at Object.compile (/Users/person/.nvm/versions/node/v5.0.0/lib/node_modules/ts-node/src/index.ts:403:17)
at loader (/Users/person/.nvm/versions/node/v5.0.0/lib/node_modules/ts-node/src/index.ts:289:33)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/person/.nvm/versions/node/v5.0.0/lib/node_modules/ts-node/src/index.ts:306:14)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at Object.<anonymous> (/Users/person/.nvm/versions/node/v5.0.0/lib/node_modules/ts-node/src/_bin.ts:179:12)
at Module._compile (module.js:425:26)


Cannot redeclare block-scoped variable 'reducer' from the tsc and I'm not sure why

If you don't have a root level import or export in your file then it is considered a global module. There is clearly another file that is also global and has the variable reducer declared.


Move to modules (commonjs is a great choice). Alternatively change let to var and that should give you an error at the location where another reducer is declared.

PS: And IDE can give a much nicer experience here giving you the other variable declaration locations upfront.