Inferring types of optional properties

13 hours ago 1
ARTICLE AD BOX
export interface ProcessedResultOptions< TField1 = undefined, TField2 = undefined > { field1?: TField1; field2?: TField2; } export class ProcessedResult< const TField1 = undefined, const TField2 = undefined > { public readonly field1: TField1; public readonly field2: TField2; constructor(options: ProcessedResultOptions<TField1, TField2>) { this.field1 = options.field1; /* Type 'TField1 | undefined' is not assignable to type 'TField1'. 'TField1' could be instantiated with an arbitrary type which could be unrelated to 'TField1 | undefined'. */ this.field2 = options.field2; } }

Why is this failing, when field1 is absent, the type of options should be ProcessedResultOptions<undefined, ...> so options.field1 (undefined) should be assignable to this.field1 (undefined)

I can fix this with a simple options.field1 as TField1 but I feel like I shouldn't have to use this.

Read Entire Article