Forum Discussion
3 Replies
- Shards632Mentor
Nullable properties, like Entity and Asset, can be 'undefined' at runtime (not `null`). So, you need to address that in your code. I would _not_ recommend just adding `!` to every prop ref willy nilly tho. That's a recipe for hiding real bugs that could be caught at compile time, and causing runtime errors.
I would be more careful about checking that all your properties are set, or filtering out unset properties from your entity array you are constructing, or something. I sometimes use filter with a type assertion to do this. Unfortunately, too hard to paste the code here w/o markdown formatting support. 😞- InaCentaurMember
Yup, nullcheck is done in the function, but it was taking me quite a long while to understand why it was not valid syntax simply to pass a this.propType.Entity to a hz.Entity argument in the function.
I also just have another function checking for nulls, that all other functions can call.
tldr; Figuring out that adding a ! was the way to valid syntax seemed really ad hoc to me.
- Shards632Mentor
Again, I would recommend _not_ using `!` in your code unless you are _very_ careful about it. It is turning off compiler warnings about type errors in your code. An entity property _can_ be undefined at runtime! So you need to either
- accept undefined as a type for your function parameter
- use a type guard / narrowing expression prior to passing the value to the function to ensure it is not undefined.