Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit dbb177e

Browse files
author
Orta
authoredJun 19, 2019
Merge pull request #154 from Reltre/js_ts_mix
Add a sample of using JS and TS together with allowJS option
2 parents 1960399 + 89f7608 commit dbb177e

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
 

‎js-and-ts/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# TypeScript Sample: Mixing TypeScript and JavaScript
2+
3+
## Overview
4+
5+
A sample of how to use the `allowJS` option to use both JavaScript and TypeScript together.
6+
A simple text formatter is provided, written in JavaScript. This formatter is then used
7+
within a TypeScript class to format a computation.
8+
9+
To run this sample, you must have `node` installed. You can also use `ts-node` to run this directly
10+
without a compilation from TypeScript to JavaScript.
11+
12+
## Running
13+
14+
```bash
15+
$ tsc robot.ts`
16+
$ node robot.js`
17+
```

‎js-and-ts/format.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const surroundWithStars = (value) => {
2+
const valueLength = value.toString().length;
3+
const topBottomBorder = '*'.repeat(valueLength + 2);
4+
5+
return topBottomBorder
6+
+ "\n"
7+
+ '*' + value.toString() + '*'
8+
+ "\n"
9+
+ topBottomBorder;
10+
}
11+
12+
module.exports.Formatter = { surroundWithStars };
13+

‎js-and-ts/robot.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// This import wouldn't be possible without the allowJS option in tsconfig
2+
import { Formatter } from './format.js';
3+
4+
interface Robot {
5+
name: String;
6+
currentComputation: Number;
7+
}
8+
9+
class Robot {
10+
constructor(public name: String) {
11+
this.name = name;
12+
this.currentComputation = 0;
13+
}
14+
15+
// Given a mathematical operation, return a value based on the value passed,
16+
// the operation and the number 10
17+
compute(operation, value) {
18+
let computedValue = 0;
19+
switch(operation) {
20+
case '+':
21+
computedValue = value + 10;
22+
break;
23+
case '-':
24+
computedValue = value - 10;
25+
break;
26+
case '/':
27+
computedValue = value / 10;
28+
break;
29+
case '*':
30+
computedValue = value * 10;
31+
break;
32+
default:
33+
console.log("Does not compute!!")
34+
}
35+
this.currentComputation = computedValue;
36+
}
37+
38+
// Using an external JS module, format the computed value from our robot
39+
displayCurrentComputation() {
40+
console.log(Formatter.surroundWithStars(this.currentComputation));
41+
}
42+
}
43+
44+
const hal = new Robot('Hal');
45+
hal.compute('+', 32);
46+
hal.displayCurrentComputation();

‎js-and-ts/tsconfig.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./built",
4+
"sourceMap": true,
5+
"allowJs": true,
6+
"target": "es6"
7+
},
8+
"include": [
9+
"./**/*"
10+
]
11+
}

0 commit comments

Comments
 (0)
This repository has been archived.