Search code examples
angulartypescriptngx-formly

How to describe formly custom field options type to pass angular strictTemplates check?


I have angular app with strictTemplates option turned on. I can't get how to describe options type to pass strcitTemplates check.

Tempalte which rise error:

<p-multiSelect [options]="$any(props.options)"
               [formControl]="formControl"
               [formlyAttributes]="field" />

Component:

import { Component } from '@angular/core';
import { FieldType, FieldTypeConfig } from '@ngx-formly/core'

@Component({
    selector: 'form-multiselect',
    templateUrl: 'multiselect.component.html',
})
export class MultiselectComponent extends FieldType<FieldTypeConfig> {

}

If you remove $any type casting in template. You get:

TS2322: Type 'any[] | Observable<any[]> | undefined' is not assignable to type 'any[] | undefined'.

I can't understand how to extend FieldType<FieldTypeConfig> type to let it know that I have array always.


Solution

  • So, I had to create my custom type instead FormlyFieldProps

    export interface CustomFormlyFieldProps extends FormlyFieldProps {
        // Problem was because in default type there is Observable<any[]> here
        options?: any[]; 
    }
    
    
    export class MultiselectComponent extends FieldType<FieldTypeConfig<CustomFormlyFieldProps>> {}