簡體   English   中英

打字稿中自定義對象的類型

[英]type of custom object in typescript

我是打字稿的新手,我創建了這個對象:

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

我想使用 is 作為 useState 的初始狀態。 因此我現在想要將我需要作為類型傳遞給我的 useState 的內容。 const [Form, setForm] = useState< "what should come here?" >(initialState) const [Form, setForm] = useState< "what should come here?" >(initialState)

提前致謝,

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

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

你可以這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM