Make Form input strongly typed with generics.
[ustaxlib.git] / src / Form.ts
1 import { Line } from './Line';
2 import { InconsistencyError, NotFoundError } from './Errors';
3
4 export default abstract class Form<I = unknown> {
5 private _lines: Line<any>[] = [];
6 private _input?: I;
7
8 abstract get name(): string;
9
10 constructor(input?: I) {
11 this._input = input;
12 this.getLines().map(this.addLine.bind(this));
13 }
14
15 protected abstract getLines(): Line<any>[];
16
17 get allowMultipleCopies(): boolean {
18 return false;
19 }
20
21 private addLine(line: Line<any>) {
22 if (line.form !== undefined) {
23 throw new InconsistencyError('Line is already in a Form');
24 }
25 try {
26 this.getLine(line.id);
27 } catch {
28 line.form = this;
29 this._lines.push(line);
30 return;
31 }
32 throw new InconsistencyError('Cannot add a line with a duplicate identifier');
33 }
34
35 getLine(id: string): Line<any> {
36 const lines = this._lines.filter(l => l.id === id);
37 if (lines.length == 0) {
38 throw new NotFoundError(id);
39 }
40 return lines[0];
41 }
42
43 getInput<K extends keyof I>(name: K): I[K] {
44 if (!(name in this._input)) {
45 throw new NotFoundError(`No input with key ${name} on form ${this.name}`);
46 }
47 return this._input[name];
48 }
49 };