-
Notifications
You must be signed in to change notification settings - Fork 36
/
typescript.ts
62 lines (49 loc) · 1.45 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* TypeScript
*/
// Single line comment
function test(arg1: string, arg2: number): void {
const string = "foo";
const number = 123;
console.log(typeof(undefined))
}
interface IColor {
}
// Other color classes look like this
export class Red {
public readonly id: number;
private readonly store: ColorStore;
private description: string;
constructor(id: number, store: ColorStore, description: string) {
this.store = store;
this.description = description;
}
}
export type AnyColor = Red | Blue | Yellow | Green | Purple | Orange;
export class ColorStore {
private _colors: AnyColor[];
private createColor<T extends AnyColor>(type: (new (store: ColorStore, id: number, description: string) => T), id: number, description: string): T {
const item = new type(this, id, description);
this.addItem(item);
return item;
}
public addItem<T extends AnyColor>(v: T): void {
if (this._colors.find((x) => x.id === v.id))
throw new Error(`The color with id ${v.id} already exists`);
this._colors.push(v);
}
public getItem<T extends AnyColor>(id: number): T | undefined {
return this._colors.find((x) => x.id === id);
}
public deleteItem<T extends AnyColor>(id: number): void {
const index = this._colors.findIndex((x) => x.id === id);
if (index < 0) return;
this._colors.splice(index, 1);
}
}
export class Person {
#SSN: number;
name: string;
age: number;
emergencyContact: Person;
}