1 import Person from './Person';
2 import TaxReturn from './TaxReturn';
3 import { Line } from './Line';
4 import { InconsistencyError, NotFoundError } from './Errors';
6 export default abstract class Form<L extends { [key: string]: Line<any> },
8 abstract readonly name: string;
10 protected abstract readonly _lines: L;
12 readonly supportsMultipleCopies: boolean = false;
14 private readonly _input?: I;
16 // Avoid using this; prefer the getLine() helpers declared below. This
17 // is only exposed for propagating line type information.
18 get lines(): L { return this._lines; }
20 constructor(input?: I) {
25 for (const id in this._lines) {
26 let l = this._lines[id];
32 person(): Person | undefined {
36 getLine<K extends keyof L>(id: K): L[K] {
37 if (!(id in this._lines))
38 throw new NotFoundError(`Form ${this.name} does not have line ${id}`);
39 return this._lines[id];
42 getValue<K extends keyof L>(tr: TaxReturn, id: K): ReturnType<L[K]['value']> {
43 const line: L[K] = this.getLine(id);
44 return line.value(tr);
47 getInput<K extends keyof I>(name: K): I[K] {
48 if (!(name in this._input)) {
49 throw new NotFoundError(`No input with key ${name} on form ${this.name}`);
51 return this._input[name];
54 hasInput<K extends keyof I>(name: K): boolean {
55 return this._input !== undefined && name in this._input;
59 export type FormClass<T extends Form<any>> = new (...args: any[]) => T;
61 export function isFormT<T extends Form<any>>(form: Form<any>,
62 formClass: FormClass<T>):
64 return form.constructor === formClass;