繁体   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