'I cannot set undefined type for fetched data by useMutation

I want to store fetched data using useState as below, but it seems something in not working with types, logic is kept in Context, data is fetched from API

const {
        lineSectionsDefectsDetailsData,
        getLineSectionsDefectsDetailsData,
    } = useContext(LineSectionsContext);

    const [data, setData] = useState<
            LineSectionsDefectWarningDetailsType | undefined
        >();
    
        useEffect(() => {
            setData(lineSectionsDefectsDetailsData);
        }, [lineSectionsDefectsDetailsData]);

enter image description here

This is the error I get from typescript:

const lineSectionsDefectsDetailsData: LineSectionsDefectWarningDetailsType[] Argument of type 'LineSectionsDefectWarningDetailsType[]' is not assignable to parameter of type 'SetStateAction<LineSectionsDefectWarningDetailsType | undefined>'.ts(2345)

What is also strange to me is that when I create Context interface and write:

lineSectionsDefectsDetailsData: LineSectionsDefectsDetailsType[] | undefined;

but when I press ctrl+s VSC saves it like in below picture which I think might also create some issue, also it's strange behaviour.

enter image description here

what is the issue and how to type initial undefined type?



Sources

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

Source: Stack Overflow

Solution Source