2016-07-27 95 views
1

我試圖從書中運行一些示例代碼,並且從tsc中收到錯誤Cannot redeclare block-scoped variable 'reducer',我不知道爲什麼?無法重新聲明塊範圍變量'reducer'(打字稿)

tsc -v 
// Version 2.1.0-dev.20160726 

然後我跑TS-節點像這樣:

ts-node 01-identity-reducer.ts 

01 - 識別 - 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) 

回答

3

不能重新聲明塊範圍的從TSC變量「減速」,我不知道爲什麼

如果你沒有在你的文件,那麼它被認爲是一個根級別的進口或出口一個全局模塊。很明顯,另一個文件是也是 global,並且聲明瞭變量reducer

修復

移動到模塊(commonjs是一個不錯的選擇)。或者將let更改爲var,並且應該在聲明另一個reducer的位置發生錯誤。

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

1

只需在其周圍輸出命名空間。 export namespace WhatYouLike { ...code... }