Further improve the typing of InputLine to not require the `as any` cast.
authorRobert Sesek <rsesek@bluestatic.org>
Thu, 20 Feb 2020 23:49:58 +0000 (18:49 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Thu, 20 Feb 2020 23:49:58 +0000 (18:49 -0500)
src/Line.test.ts
src/Line.ts
src/fed2019/FormW2.ts

index 0d658c275452b43660cfd677de313771b5f37314..f49b29485be5faee6e8bee95cbb2c8500f36d738 100644 (file)
@@ -62,8 +62,8 @@ test('input line', () => {
 
     protected getLines() {
       return [
-        new InputLine<string, Input>('1', 'key'),
-        new InputLine<string, Input>('2', 'key2')
+        new InputLine<Input>('1', 'key'),
+        new InputLine<Input>('2', 'key2')
       ];
     }
   };
index 212fd6172bc87fcad7d41093fe531ef528d54441..5e4773b05d4fbfc83ae37702f13f2814b5fc3e40 100644 (file)
@@ -53,18 +53,18 @@ export class ReferenceLine<T> extends Line<T> {
   }
 };
 
-export class InputLine<T, U = unknown> extends Line<T> {
-  private _input: keyof U;
+export class InputLine<U = unknown, T extends keyof U = any> extends Line<U[T]> {
+  private _input: T;
 
   form: Form<U>;
 
-  constructor(id: string, input: keyof U, description?: string) {
+  constructor(id: string, input: T, description?: string) {
     super(id, description);
     this._input = input;
   }
 
-  value(tr: TaxReturn): T {
-    return this.form.getInput(this._input) as any;
+  value(tr: TaxReturn): U[T] {
+    return this.form.getInput<T>(this._input);
   }
 };
 
index a0ca2d9f14e21c7647fa5b7c93659469ba949119..bb0c0ec72f29fd99547e5dd5628e1e91d1f9b7de 100644 (file)
@@ -31,7 +31,7 @@ export interface W2Input {
   box14?: CodeAndAmount[];
 };
 
-class Input<T> extends InputLine<T, W2Input> {};
+class Input<T extends keyof W2Input> extends InputLine<W2Input, T> {};
 
 export default class W2 extends Form<W2Input> implements SupportsMultipleCopies {
   get name(): string { return 'W-2'; }
@@ -40,21 +40,21 @@ export default class W2 extends Form<W2Input> implements SupportsMultipleCopies
 
   protected getLines(): Line<any>[] {
     return [
-      new Input<string>('c', 'employer', 'Employer name'),
-      new Input<Person>('e', 'employee', 'Emplyee name'),
-      new Input<number>('1', 'wages', 'Wages, tips, other compensation'),
-      new Input<number>('2', 'fedIncomeTax', 'Federal income tax withheld'),
-      new Input<number>('3', 'socialSecurityWages', 'Social security wages'),
-      new Input<number>('4', 'socialSecuirtyTax', 'Social security tax withheld'),
-      new Input<number>('5', 'medicareWages', 'Medicare wages and tips'),
-      new Input<number>('6', 'medicareTax', 'Medicare tax withheld'),
-      new Input<number>('7', 'socialSecurityTips', 'Social security tips'),
-      new Input<number>('8', 'allocatedTips', 'Allocated tips'),
-      new Input<number>('10', 'dependentCareBenefits', 'Dependent care benefits'),
-      new Input<number>('11', 'nonqualifiedPlans','Nonqualified plans'),
-      new Input<CodeAndAmount[]>('12', 'box12', 'Box 12'),
-      new Input<Box13>('13', 'box13', 'Box 13'),
-      new Input<CodeAndAmount[]>('14', 'box14', 'Other'),
+      new Input('c', 'employer', 'Employer name'),
+      new Input('e', 'employee', 'Emplyee name'),
+      new Input('1', 'wages', 'Wages, tips, other compensation'),
+      new Input('2', 'fedIncomeTax', 'Federal income tax withheld'),
+      new Input('3', 'socialSecurityWages', 'Social security wages'),
+      new Input('4', 'socialSecuirtyTax', 'Social security tax withheld'),
+      new Input('5', 'medicareWages', 'Medicare wages and tips'),
+      new Input('6', 'medicareTax', 'Medicare tax withheld'),
+      new Input('7', 'socialSecurityTips', 'Social security tips'),
+      new Input('8', 'allocatedTips', 'Allocated tips'),
+      new Input('10', 'dependentCareBenefits', 'Dependent care benefits'),
+      new Input('11', 'nonqualifiedPlans','Nonqualified plans'),
+      new Input('12', 'box12', 'Box 12'),
+      new Input('13', 'box13', 'Box 13'),
+      new Input('14', 'box14', 'Other'),
     ];
   }
 };