Staff Software Engineer #OpenToWork, Formerly @snyksec, @bbc, @adastralpark (BT Labs), @ShrimpingIt,@vgkits. Sculpting open source hard+software, 3rd line support for others.
Hi, Carlo. Thought it was worth pointing out a latent bug in the example. Probably as RootState should never be used. You can see and experiment with the error at tsplay.dev/WkM3lN
Essentially the clause as RootState is a type assertion, when what is probably intended is for that data structure to be validated as a RootState by the compiler. Normally the keywords as and any are a sign types are being bypassed (with the exception of as const).
The type assertion is hiding an error in the stateFactory2 example below, but the compiler can't pick that up, since you have asserted the type and hence suspended the validation of the compiler.
The example stateFactory3 below is preferred, and DOES reveal the bug as a compiler error.
Hey Cefn, thank you so much for pointing this out! I haven't actually gotten into details regarding this so I highly appreciate your comment as it helps out for the other readers as well
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi, Carlo. Thought it was worth pointing out a latent bug in the example. Probably
as RootState
should never be used. You can see and experiment with the error at tsplay.dev/WkM3lNEssentially the clause
as RootState
is a type assertion, when what is probably intended is for that data structure to be validated as aRootState
by the compiler. Normally the keywordsas
andany
are a sign types are being bypassed (with the exception ofas const
).The type assertion is hiding an error in the stateFactory2 example below, but the compiler can't pick that up, since you have asserted the type and hence suspended the validation of the compiler.
The example stateFactory3 below is preferred, and DOES reveal the bug as a compiler error.
Hey Cefn, thank you so much for pointing this out! I haven't actually gotten into details regarding this so I highly appreciate your comment as it helps out for the other readers as well