-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathTypeScript.ts
48 lines (41 loc) · 1.17 KB
/
TypeScript.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Running on: https://www.typescriptlang.org/play
class TSPrinter {
private _text: String = "Hello World!";
private _standardOut: StandardOut = StandardOut.ALERT;
public get standardOut(): StandardOut {
return this._standardOut;
}
public set standardOut(standardOut: StandardOut) {
this._standardOut = standardOut;
}
public get text(): String {
return this._text;
}
public set text(text: String) {
this._text = text;
}
public printText(): void {
switch (this.standardOut) {
case StandardOut.ALERT:
window.alert(this.text);
break;
case StandardOut.CONSOLE:
console.log(this.text);
break;
default:
throw new Error(`Unexpected standard output: ${this.standardOut}!`);
}
}
}
enum StandardOut {
CONSOLE,
ALERT,
UNKNOWN, // for provoking an error
}
const printer = new TSPrinter();
printer.standardOut = StandardOut.CONSOLE;
try {
printer.printText();
} catch (e) {
console.error(`${Date.now().toLocaleString()}: Error while printing: '${printer.text}'. ${e}`);
}