【问题标题】:How to assign an interface to a const如何将接口分配给 const
【发布时间】:2020-08-18 11:10:40
【问题描述】:

我对 Typescript 很陌生,在我的 const issuesInitialState 中,我正在尝试分配接口 IssuesInitialState,但出现以下错误。

类型“{}”缺少类型“问题”的以下属性:closeDate、description、issueId、lastEditDate 和 6 more.ts(2740)

预期的类型来自属性issue,它在此处声明类型为IssuesInitialState

export interface Issue {
  closeDate: string | null;
  description: string;
  issueId: number;
  lastEditDate: string | null;
  priorityId: string;
  projectId: number;
  reportDate: string;
  statusId: string;
  title: string;
  userId: number;
}

interface IssuesInitialState{
  issues:Issue[];
  issue:Issue; 
  issuesByProject:Issue[];
  updateIssue:Issue;
}

export const issuesInitialState:IssuesInitialState = {
  issues: [],
  issue:{},
  issuesByProject: [],
  updateIssue: {},
};

这样我就可以将强制转换状态键入IssuesInitialState

export const issuesReducer = (
  state = issuesInitialState,
  action: { type: string; payload: Issue }
) :{}=> {
  switch (action.type) {
.....

【问题讨论】:

    标签: reactjs typescript tsx


    【解决方案1】:

    你的Issue 有属性,所以在你的const 中,它不能被声明为{}。如果变量的类型是具有属性的对象,则必须在声明变量时声明这些属性。

    希望它足够清楚。祝你好运!

    【讨论】:

    • 您好,如果我希望我的问题具有 typeof 问题,我将如何声明它?
    • 我不确定,但也许它被定义为一个类。
    猜你喜欢
    • 2018-12-07
    • 2013-10-15
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    相关资源
    最近更新 更多