0

I have an error with error on defaultState const:

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

The error is:

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; }'.

I can't get everything ok with types, any hint here?

1
  • What do you want to do exactly? here you're saying that defaultState is an object with just one property named auth which can be either of type AuthState or null. but you're adding properties like loading and error to your object. Commented Nov 5, 2020 at 12:48

1 Answer 1

1

Try this one:

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

What you did before was saying that auth is either null or AuthState. But I guess what you wanted to say was: defaultState is either { auth: null } or AuthState.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.