You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you now try to create a user without passing a status then the compiler complains:
constuser=newUser({name: 'John'})/*Argument of type '{ name: string; }' is not assignable to parameter of type 'EntityData<UserType>'. Property 'status' is missing in type '{ name: string; }' but required in type 'EntityData<UserType>'.ts(2345)*/
According to the documentation this can be solved by making the status property optional in the interface:
interfaceUserType{name: string;status?: string;}constuser=newUser({name: 'John'})// OK
However, now I'm forced to tell the compiler everywhere the status property is used that it can't be undefined:
conststatus: string=user.status// Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.ts(2322)// The following works of course but it's annoyingconststatus: string=user.status!
Is there a better way to handle this use case?
The text was updated successfully, but these errors were encountered:
As far as I know, this is how it should be. If you create a user with
constuser=newUser({name: 'John'})
That user does not have a status, it this then undefined. If your logic updates the status, you are the only one that knows it for sure and that's why Typescript lets you add an ! behind.
Hello,
Consider the following definition of a
User
entity:If you now try to create a user without passing a
status
then the compiler complains:According to the documentation this can be solved by making the
status
property optional in the interface:However, now I'm forced to tell the compiler everywhere the
status
property is used that it can't beundefined
:Is there a better way to handle this use case?
The text was updated successfully, but these errors were encountered: