linkedSignal typed returning error due to being undefined

Discussão em 'Angular' iniciado por st_clair_clarke, Janeiro 7, 2025.

  1. I have the following:

    type TSelectOptions = {
    badge?: StringType
    disabled: boolean
    expanded?: BooleanType
    group?: string | undefined
    icon?: StringType
    id: TID
    image?: StringType
    index?: NumberType
    info?: ArrayType<string>
    iso?: ArrayType<string>
    key: string
    items: TSelectItem[]
    selected?: boolean
    value: string // number | string
    view?: StringType
    }

    export type TSelectItem = {
    id: TID
    disabled?: boolean
    emoji?: string
    icon?: string
    iso?: string
    key?: string
    selected?: boolean
    value: string
    view: string
    }

    export class SelectItem {
    id: TID = ID()

    disabled = false
    emoji = ''
    icon = ''
    selected = false
    view = ''
    value = ''
    }

    export type TSelectItemGroup = TSelectItem & Pick<TSelectOptions, 'items'>
    // Pick<TSelectOptions, 'key' | 'icon' | 'items' | 'selected'>



    $titlesResrc: ResourceRef<TSelectItemGroup[]> = this.#resrcFacade.configureResource(
    'registration',
    'titles.json'
    )


    $titles: WritableSignal<TSelectItemGroup[]> = signal<TSelectItemGroup[]>([])

    protected $titlesinked = linkedSignal({
    source: (): ResourceRef<TSelectItemGroup[]> => this.$titlesResrc,
    computation: () =>this.$titles.set( this.$titlesResrc ? this.$titlesResrc.value() : [])
    })



    The last line in $titlesLinked property:

    computation: () =>this.$titles.set( this.$titlesResrc ? this.$titlesResrc.value() : [])


    always results in the following error:

    - error TS2345: Argument of type 'TSelectItemGroup[] | undefined' is not assignable to parameter of type 'TSelectItemGroup[]'.
    Type 'undefined' is not assignable to type 'TSelectItemGroup[]'.

    165 computation: () =>this.$titles.set( this.$titlesResrc ? this.$titlesResrc.value() : [])



    I need some help in previnting the initial undefined which seems to generate the problem.

    Continue reading...

Compartilhe esta Página