From 41489179e9064c876500f1b1e6dc0bc52afceba6 Mon Sep 17 00:00:00 2001 From: Reltre Date: Fri, 26 Oct 2018 14:17:52 -0700 Subject: [PATCH 1/5] Add tsconfig which allows javascript files. --- js-and-ts/tsconfig.json | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 js-and-ts/tsconfig.json diff --git a/js-and-ts/tsconfig.json b/js-and-ts/tsconfig.json new file mode 100644 index 0000000..2a9bebf --- /dev/null +++ b/js-and-ts/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "outDir": "./built", + "sourceMap": true, + "allowJs": true, + "target": "es6" + }, + "include": [ + "./**/*" + ] +} \ No newline at end of file From f0db5e7a9c3374f66728c7bfecc008111891fbf9 Mon Sep 17 00:00:00 2001 From: Reltre Date: Fri, 26 Oct 2018 14:18:23 -0700 Subject: [PATCH 2/5] Create simple class which requires an existing js module. --- js-and-ts/robot.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 js-and-ts/robot.ts diff --git a/js-and-ts/robot.ts b/js-and-ts/robot.ts new file mode 100644 index 0000000..611a076 --- /dev/null +++ b/js-and-ts/robot.ts @@ -0,0 +1,40 @@ +// This import wouldn't be possible without the allowJS option in tsconfig +import { Formatter } from './format.js'; + +interface Robot { + name: String, + currentComputation: Number +} + +class Robot { + constructor(public name: String) { + this.name = name; + this.currentComputation = 0; + } + + // Given a mathematical operation, return a value based on the value passed, + // the operation and the number 10 + compute(operation, value) { + let computedValue = 0; + switch(operation) { + case '+': + computedValue = value + 10 + case '-': + computedValue = value - 10 + case '/': + computedValue = value / 10 + case '*': + computedValue = value * 10 + } + this.currentComputation = computedValue; + } + + // Using an external JS module, format the computed value from our robot + displayCurrentComputation() { + console.log(Formatter.surroundWithStars(this.currentComputation)); + } +} + +const hal = new Robot('Hal'); +hal.compute('+', 32); +hal.displayCurrentComputation(); \ No newline at end of file From e3b5661a5af713ce09215f4aca24454caec87bc2 Mon Sep 17 00:00:00 2001 From: Reltre Date: Fri, 26 Oct 2018 14:19:12 -0700 Subject: [PATCH 3/5] A simple module, to be used as a mixin to a ts file. --- js-and-ts/format.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 js-and-ts/format.js diff --git a/js-and-ts/format.js b/js-and-ts/format.js new file mode 100644 index 0000000..9b6ebd7 --- /dev/null +++ b/js-and-ts/format.js @@ -0,0 +1,13 @@ +const surroundWithStars = (value) => { + const valueLength = value.toString().length; + const topBottomBorder = '*'.repeat(valueLength + 2); + + return topBottomBorder + + "\n" + + '*' + value.toString() + '*' + + "\n" + + topBottomBorder; +} + +module.exports.Formatter = { surroundWithStars }; + From f0bc0f2eba9b320650d06f97864daca5e7495e5c Mon Sep 17 00:00:00 2001 From: Reltre Date: Fri, 26 Oct 2018 14:38:18 -0700 Subject: [PATCH 4/5] Fix issues in robot class. --- js-and-ts/robot.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/js-and-ts/robot.ts b/js-and-ts/robot.ts index 611a076..0542c12 100644 --- a/js-and-ts/robot.ts +++ b/js-and-ts/robot.ts @@ -2,8 +2,8 @@ import { Formatter } from './format.js'; interface Robot { - name: String, - currentComputation: Number + name: String; + currentComputation: Number; } class Robot { @@ -18,13 +18,19 @@ class Robot { let computedValue = 0; switch(operation) { case '+': - computedValue = value + 10 + computedValue = value + 10; + break; case '-': - computedValue = value - 10 + computedValue = value - 10; + break; case '/': - computedValue = value / 10 + computedValue = value / 10; + break; case '*': - computedValue = value * 10 + computedValue = value * 10; + break; + default: + console.log("Does not compute!!") } this.currentComputation = computedValue; } From 89f7608ee91c2de1117bbf382ba83b74c547d3ca Mon Sep 17 00:00:00 2001 From: Reltre Date: Fri, 26 Oct 2018 14:38:52 -0700 Subject: [PATCH 5/5] Add readme to 'ts-and-js' sample folder. --- js-and-ts/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 js-and-ts/README.md diff --git a/js-and-ts/README.md b/js-and-ts/README.md new file mode 100644 index 0000000..2b3ecfb --- /dev/null +++ b/js-and-ts/README.md @@ -0,0 +1,17 @@ +# TypeScript Sample: Mixing TypeScript and JavaScript + +## Overview + +A sample of how to use the `allowJS` option to use both JavaScript and TypeScript together. +A simple text formatter is provided, written in JavaScript. This formatter is then used +within a TypeScript class to format a computation. + +To run this sample, you must have `node` installed. You can also use `ts-node` to run this directly +without a compilation from TypeScript to JavaScript. + +## Running + +```bash +$ tsc robot.ts` +$ node robot.js` +``` \ No newline at end of file