B3th4
B3th4

Reputation: 113

type of custom object in typescript

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,

Upvotes: 0

Views: 77

Answers (2)

Yatin Gaikwad
Yatin Gaikwad

Reputation: 1200

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);

Upvotes: 1

Saka Sai Trinath
Saka Sai Trinath

Reputation: 306

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

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

Upvotes: 2

Related Questions