winnekes / itypescript

ITypescript is a typescript kernel for the Jupyter notebook (A modified version of IJavascript)
Other
190 stars 29 forks source link

Issue with variable scoping using (IIFE) #17

Open nesterow opened 4 years ago

nesterow commented 4 years ago

Hello, This issue related to variable scoping and default behaviour described in the doc. As described in the doc I cannot re-declare constants and classes or execute the same cell twice.

ITypescript cannot know which cell is currently executing, you cannot redeclare the variables if you define with let or equivalent keywords

IJavaScript has the same behaviour, but I can get around it by using IIFE:

(() => {
    class Something {
        constructor(test){
            console.log(test)
        }
    }
    const test = 23
    console.log(test)
    new Something('test')
})()

Now, I don't know if it is specific to TypeScript, but apparently I cannot use the same trick with ITypeScript:

(() => {
   class Example {
    constructor(test = "test") {
        console.log(test);
    }
   }

    new Example();
    new Example("It works!"); 
})()

On the first call I receive expected result. But if try second time I receive following error:

Error: Conflict with a committed line: 
(() => {
^
TS2349: This expression is not callable.
  Type 'void' has no call signatures.
    at execTranspile (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/lib/kernel.js:203:27)
    at Session.transpiler [as transpile] (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/lib/kernel.js:263:37)
    at Session._runNow (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:796:39)
    at Session._run (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:766:14)
    at Session.execute (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/node_modules/nel/lib/nel.js:890:10)
    at Kernel.execute_request (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/node_modules/jp-kernel/lib/handlers_v5.js:116:18)
    at Kernel.onShellMessage (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/node_modules/jp-kernel/lib/jp-kernel.js:285:41)
    at Socket.<anonymous> (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/node_modules/jmp/index.js:350:17)
    at Socket.emit (events.js:198:13)
    at Socket._emitMessage (/Users/nesterow/.nvm/versions/node/v10.17.0/lib/node_modules/itypescript/node_modules/zeromq/lib/index.js:640:15)