1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
6 import Person from './Person';
7 import TaxReturn from './TaxReturn';
8 import Trace from './Trace';
9 import { Line } from './Line';
10 import { InconsistencyError, NotFoundError } from './Errors';
12 export default abstract class Form<L extends { [key: string]: Line<any> },
14 abstract readonly name: string;
16 protected abstract readonly _lines: L;
18 readonly supportsMultipleCopies: boolean = false;
20 private readonly _input?: I;
22 // Avoid using this; prefer the getLine() helpers declared below. This
23 // is only exposed for propagating line type information.
24 get lines(): L { return this._lines; }
26 constructor(input?: I) {
31 for (const id in this._lines) {
32 let l = this._lines[id];
38 person(): Person | undefined {
42 getLine<K extends keyof L>(id: K): L[K] {
43 if (!(id in this._lines))
44 throw new NotFoundError(`Form ${this.name} does not have line ${id}`);
45 return this._lines[id];
48 getValue<K extends keyof L>(tr: TaxReturn, id: K): ReturnType<L[K]['value']> {
49 const line: L[K] = this.getLine(id);
50 return line.value(tr);
53 getInput<K extends keyof I>(name: K): I[K] {
54 if (!(name in this._input)) {
55 throw new NotFoundError(`No input with key ${name} on form ${this.name}`);
57 Trace.add(`${this.name} input: ${name}`);
58 return this._input[name];
61 hasInput<K extends keyof I>(name: K): boolean {
62 return this._input !== undefined && name in this._input;
66 export type FormClass<T extends Form<any>> = new (...args: any[]) => T;
68 export function isFormT<T extends Form<any>>(form: Form<any>,
69 formClass: FormClass<T>):
71 return form.constructor === formClass;