From 26177f7e25370d27e6e2f04857269129a74d1f44 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 19 Feb 2020 21:29:09 -0500 Subject: [PATCH] Make SupportsMultipleCopies an interface. --- src/Form.ts | 12 ++++++++---- src/TaxReturn.test.ts | 6 +++--- src/TaxReturn.ts | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Form.ts b/src/Form.ts index 507a611..07e9ccc 100644 --- a/src/Form.ts +++ b/src/Form.ts @@ -14,10 +14,6 @@ export default abstract class Form { protected abstract getLines(): Line[]; - get allowMultipleCopies(): boolean { - return false; - } - private addLine(line: Line) { if (line.form !== undefined) { throw new InconsistencyError('Line is already in a Form'); @@ -47,3 +43,11 @@ export default abstract class Form { 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; +}; diff --git a/src/TaxReturn.test.ts b/src/TaxReturn.test.ts index 74c82f4..852ae65 100644 --- a/src/TaxReturn.test.ts +++ b/src/TaxReturn.test.ts @@ -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 []; } }; diff --git a/src/TaxReturn.ts b/src/TaxReturn.ts index 18a0bbb..7bab4d8 100644 --- a/src/TaxReturn.ts +++ b/src/TaxReturn.ts @@ -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}`); -- 2.43.5