0

I'm new to typescript and I created this object:

const initialState = {
    title: {
        value: "",
        error: false
    },
    amount: {
        value: 0,
        error: false
    },
    type: "Food",
    time: new Date()
} 

I want to use is as initial state of a useState. therefore I want to now what I need to passe as type to my useState. const [Form, setForm] = useState< "what should come here?" >(initialState);

thanks in advance,

2 Answers 2

2
interface IState {
  title: {
    value: string;
    error: boolean;
  };
  amount: {
    value: number;
    error: boolean;
  };
  type: string;
  time: Date;
}

const [Form, setForm] = useState<IState>(initialState);
Sign up to request clarification or add additional context in comments.

Comments

1

You could do something like this:

interface SomeName {
    value: string;
    error: boolean;
}

interface IState {
  title: SomeName;
  amount: SomeName;
  type: string;
  time: Date;
}

const initialState: IState = {
    title: {
       value: "",
       error: false
    },
    amount: {
       value: 0,
       error: false
    },
    type: "Food",
    time: new Date()
} 

const [Form, setForm] = useState<IState>(initialState);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.