Make SupportsMultipleCopies an interface.
authorRobert Sesek <rsesek@bluestatic.org>
Thu, 20 Feb 2020 02:29:09 +0000 (21:29 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Thu, 20 Feb 2020 02:29:09 +0000 (21:29 -0500)
src/Form.ts
src/TaxReturn.test.ts
src/TaxReturn.ts

index 507a6112e1a75d2b96bc93294998125013abf566..07e9ccc6c7d0fe8ebcf408116f6c0e6d956dd85a 100644 (file)
@@ -14,10 +14,6 @@ export default abstract class Form<I = unknown> {
 
   protected abstract getLines(): Line<any>[];
 
-  get allowMultipleCopies(): boolean {
-    return false;
-  }
-
   private addLine(line: Line<any>) {
     if (line.form !== undefined) {
       throw new InconsistencyError('Line is already in a Form');
@@ -47,3 +43,11 @@ export default abstract class Form<I = unknown> {
     return this._input[name];
   }
 };
+
+export interface SupportsMultipleCopies extends Form {
+  aggregate(forms: Form[]): this;
+};
+
+export function supportsMultipleCopies(f: object): f is SupportsMultipleCopies {
+  return f instanceof Form && 'aggregate' in f;
+};
index 74c82f427f74b60401947e3ddb11c5913dabd26f..852ae6568809a935c4d1151d07329d56ba5aef2b 100644 (file)
@@ -1,6 +1,6 @@
 import TaxReturn from './TaxReturn';
 import { Person } from './Person';
-import Form from './Form';
+import Form, { SupportsMultipleCopies } from './Form';
 import { NotFoundError, InconsistencyError } from './Errors';
 
 test('constructor', () => {
@@ -67,10 +67,10 @@ test('single-copy forms', () => {
 });
 
 test('multiple-copy forms', () => {
-  class TestForm extends Form {
+  class TestForm extends Form implements SupportsMultipleCopies {
     get name(): string { return 'Test Form'; }
 
-    get allowMultipleCopies(): boolean { return true; }
+    aggregate(forms: Form[]): this { return null; }
 
     protected getLines() { return []; }
   };
index 18a0bbb031d9d3e5329bf03db8f4e299f9ab451a..7bab4d8f374de592bf62fe8dc0286a5ced81c77f 100644 (file)
@@ -1,4 +1,4 @@
-import Form from './Form';
+import Form, { SupportsMultipleCopies, supportsMultipleCopies } from './Form';
 import { Person, Relation } from './Person';
 import { NotFoundError, InconsistencyError, UnsupportedFeatureError } from './Errors';
 
@@ -38,7 +38,7 @@ export default class TaxReturn {
   }
 
   addForm(form: Form) {
-    if (!form.allowMultipleCopies) {
+    if (!supportsMultipleCopies(form)) {
       const other = this.getForms(form.name);
       if (other.length > 0) {
         throw new InconsistencyError(`Cannot have more than one type of form ${form.name}`);