Home > Software engineering >  Typescript error argument of type {} is not assignable to parameter of type User | User () => Use
Typescript error argument of type {} is not assignable to parameter of type User | User () => Use

Time:03-07

Checked other stackoverflow posts regarding this error and I still dont get it. This block of code is getting the error.

const [user, setUser] = useState<User>({})

I mapping over card component that needs the props from the user data. Why isn't this working?

Also i have a home page that grabs all the user with this block of code and i dont get the type error.

const [users, setUsers] = useState<User[]>([])

Why am i not getting such error on that?

CodePudding user response:

In your first example, your object is missing the required properties or is not instanceof User.

In your 2nd example, thats just an empty array so that will work. In that case, User describes the type requirement for the items.

  • Related