05-04-2025 06:43 PM
Debugging this took way too long for the wrong reason, for this Unity C# dev not familiar with typescript norms, so sharing this PSA
suppose you have a function that creates an entityArray from prop entities - you have to both | null the function definition argument and also add a ! when you pass the variable when calling the function:
private makeEntityArray(entity: hz.Entity | null){
}
makeEntityArray(this.propType.testEntity!);
05-05-2025 10:38 AM
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. 😞
05-05-2025 01:42 PM
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.
05-05-2025 03:44 PM
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.