Expected 1 arguments, but got 0. TS2554 - Optional Object Properties but not Object?

Viewed 402

I believe all the object properties are optional, so I do not fully understand the errors. Because I've made the properties optional, but not the object itself?

Error

Expected 1 arguments, but got 0.  TS2554

    71 |   // Get the claims on the initial page load
    72 |   useEffect(() => {
  > 73 |     fetchClaimDetails();
       |     ^
    74 |   }, []);
    75 | 
    76 |   return (

Component

 interface fetchClaimDetailsQueryParams {
  status?: string | null;
  clientClaimId?: string | null;
}

const fetchClaimDetails = async ({
  clientClaimId = null,
  status = null
}: fetchClaimDetailsQueryParams) => {

......

useEffect(() => {
  fetchClaimDetails();
}, []);
2 Answers

Yes. Making the object optional will not fix the error, as the object is being destructured in the parameter list. You need something like this:

const fetchClaimDetails = async (paramsObj: fetchClaimDetailsQueryParams = {
  clientClaimId: null,
  status: null
}) => {
  const {
    clientClaimId,
    status
  } = paramsObject;
}

This should make the parameter optional while providing a valid default value.

Even if every property of the object are optional, the object itself is required. If you truly want it to be optional, provide a default value of {} as the value for the object.

const fetchClaimDetails = async ({
  clientClaimId = null,
  status = null
}: fetchClaimDetailsQueryParams = {}) => { ... };
Related