Home > Blockchain >  Type 'boolean' is not assignable to type 'false'.ts(2345)
Type 'boolean' is not assignable to type 'false'.ts(2345)

Time:10-09

type Question= {
  id: string;
  author: {
    name: string;
    avatar: string;
  };
  content: string;
  isAnswered: false;

  isHighlighted: false;
};
 const [question, setQuestion] = useState<Question[]>([]);

i get this error:

Argument of type '{ id: string; content: string; author: { name: string; avatar: string; }; isHighlighted: boolean; isAnswered: boolean; }[]' is not assignable to parameter of type 'SetStateAction<Question[]>'. Type '{ id: string; content: string; author: { name: string; avatar: string; }; isHighlighted: boolean; isAnswered: boolean; }[]' is not assignable to type 'Question[]'. Type '{ id: string; content: string; author: { name: string; avatar: string; }; isHighlighted: boolean; isAnswered: boolean; }' is not assignable to type 'Question'. Types of property 'isAnswered' are incompatible. Type 'boolean' is not assignable to type 'false'.ts(2345)

How can i solve that?

CodePudding user response:

Are isAnswered and isHighlighted properties of Question always going to be false??, otherwise type Question should be edited to this:

    type Question= {
  id: string;
  author: {
    name: string;
    avatar: string;
  };
  content: string;
  isAnswered: boolean;

  isHighlighted: boolean;
};

CodePudding user response:

As with this question about default values in types you cannot set a 'value' for a type because it is about defining what possible values could be not the actual value. A house plan vs. a built house analogy.

It might be possible something similar to this is what you are looking for where you have a constant setting default values (i have copied their example here below).

// using typeof as a shortcut; note that it hoists!
// you can also declare the type of DefaultProps if you choose
// e.g. https://github.com/typescript-cheatsheets/react/issues/415#issuecomment-841223219
type GreetProps = { age: number } & typeof defaultProps;

const defaultProps = {
  age: 21,
};

const Greet = (props: GreetProps) => {
  // etc
};
Greet.defaultProps = defaultProps;

  • Related