类型null不能赋值给类型IJobDetails(这是我的接口)。

4

enter image description here

我创建了一个名为ClientState1的接口。 现在我想创建一个类型为ClientState1的常量descriptionJobDetails,但是它会出现如图所示的错误。

export interface ClientState1<State> {
      state: State;
      loading: boolean;
      error: any;
}

export interface IJobDetails {
    id: number;
    aboutTheCompany: string;
}

    const descriptionJobDetails: ClientState1<IJobDetails> = { state: null, loading: false, error: '' };
1个回答

14

您正在使用 strictNullChecksstrict,此选项禁止将 null 分配给未明确接受 null 的类型。

如果要将 null 分配给字段,则可以显式标记该字段接受 null

interface ClientState1<State> {
    state: State | null;
    loading: boolean;
    error: any;
}

或者如果你只是想因为某些原因在这个字段中这一次放置null,你可以使用非空断言运算符(!):

const descriptionJobDetails: ClientState1<IJobDetails> = { state: null!, loading: false, error: '' };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接