Videos
Im looking for something like this:
a standalone application where you can open a target project dir
type some TS in a code section
run it and see output in a result section
edit, rerun see new result etc...
Does something like this exist? It would need to take the code and pass it through something like `npx ts-node src/tinker.ts` and format output to something nice.
Maybe it could be VS code plugin. I just miss some "tinkering pad". I used to use this tool all the time: https://tinkerwell.app/ (PHP).
Any recommendations?
» npm install local-ts-repl
Node.js does not have a leaky global like the browser window object.
To use TypeScript code in node.js you need to use commonjs and export the class i.e.
class Foo{
public echo(){
console.log("foo");
}
}
export = Foo;
Then in the REPL:
$ node
> var Foo = require('./foo.js');
{}
> f = new Foo();
To learn more about AMD / CommonJS : https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
Update for modern times: You can use ES modules easily thanks to esm:
export class Foo{
public echo(){
console.log("foo");
}
}
In the REPL:
$ node -r esm
> import { Foo } from './foo.js';
{}
> f = new Foo();
One way I've found to do this is as follows:
$ ts-node
> import * as abc from './my-file'
> abc.myFunction()
> "works!"
If you need something that is auto-imported on each REPL session, you could expose your app in a file and then use repl.start inside that file. For example, create console.ts like this:
import repl from 'repl';
import * as models from './models';
Object.keys(models).forEach((modelName) => {
global[modelName] = models[modelName];
});
const replServer = repl.start({
prompt: 'app > ',
});
replServer.context.db = models;
And run the console using
$ node --require ts-node/register/transpile-only --experimental-repl-await console
More details here