Home > Software design >  TS 2339 Property 'property 1' does not exist on type 'IReturnState'.ts for custo
TS 2339 Property 'property 1' does not exist on type 'IReturnState'.ts for custo

Time:12-25

I have a custom hook like this:

interface IReturnState {
  activeItemsNumber: React.MutableRefObject<number>;
};

export const useMyState = (items): IReturnState => {
  // some code
  return {
    activeItemsNumber
  };
};

When I try to use it in the page component, I cannot return a custom variable name like this:

const { number1 } = useDndState(widgets);

There is an error on number1:

Property 'number1' does not exist on type 'IReturnState'.ts(2339)

I have to return the same variable name: activeItemsNumber, which means I cannot reuse the custom hook to return a different variable name. What I want to do is this:

const { number1 } = useDndState(widgets);
const { number2 } = useDndState(channels);

Any idea how to do the above?

CodePudding user response:

you can rename the variables like this:

const { activeItemsNumber: number1 } = useDndState(widgets);
const { activeItemsNumber: number2 } = useDndState(channels);

or you can return a tuple instead of an object from the custom hook

export const useMyState = (items): IReturnState => {
  // some code
  return [activeItemsNumber];
};

and then you can do

const [ number1 ] = useDndState(widgets);
const [ number2 ] = useDndState(channels);
  • Related