'TypeError: _initialState$diction.filter is not a function

I am using this code to get the system dictionary options:

export function getOptions(dicType:string,initialState:any): API.Dictionary[]|undefined {
    let dic = initialState?.dictionary?.filter(((item: { dict_type: string; }) => item.dict_type === dicType));
    return dic;
}

sometimes when I run this code, shows error:

TypeError: _initialState$diction.filter is not a function

I am confusing that, I have using the ? operator, if the result was undefined or null, the filter will not be executed, why still throw the error info? what should I do to fix this problem? I am sure the error come from here:

enter image description here

Also have tried like this:

export function getOptions(dicType:string,initialState:any): API.Dictionary[]|undefined {
    if(initialState&&initialState.dictionary&&initialState.dictionary.length>0){
        let dic = initialState.dictionary.filter(((item: { dict_type: string; }) => item.dict_type === dicType));
        return dic;
    }
    return undefined;
}

still have this error:

TypeError: initialState.dictionary.filter is not a function


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source