代码之家  ›  专栏  ›  技术社区  ›  pmiranda

Typescript,接口类型错误为空

  •  0
  • pmiranda  · 技术社区  · 5 年前

    error defaultState

    interface AuthState<T = any> {
      auth: T;
      error: null | Error;
      loading: boolean;
    }
    
    export const defaultState: { auth: null | AuthState } = {
      auth: null,
      error: null, // Here I got the error
      loading: false
    };
    

    错误是:

    TS2322: Type '{ auth: null; error: null; loading: boolean; }' is not assignable to type '{ auth: AuthState<any> | null; }'.   Object literal may only specify known properties, and 'error' does not exist in type '{ auth: AuthState<any> | null; }'.
    

    我不能把所有的事情都处理好,这里有什么提示吗?

    0 回复  |  直到 5 年前
        1
  •  1
  •   acincognito    5 年前

    interface AuthState<T = any> {
      auth: T;
      error: null | Error;
      loading: boolean;
    }
    
    export const defaultState: { auth: null } | AuthState = {
      auth: null,
      error: null, // Here I got the error
      loading: false
    };
    

    你以前是这么说的 auth 或者 null AuthState . 但我想你想说的是: defaultState 或者 { auth: null } .