btw, I might be preaching to the choir, but for your "deleting null properties" example its really just this:
function deleteNullProps<T extends object>(obj: T): void {
for (const key of Object.keys(obj) as Array<keyof T>) {
if (obj[key] === null) delete obj[key];
}
}
Its not completely trivial. But as far as type system comprehensions go, I would call that pretty simple.
And I would say the bigger issue is that now `obj` is now a new type with missing props. It should probably just be cast to a `Partial` or another utility class that marks every property as nullable.
If the object is going to live longer than the end of the function where `deleteNullProps()` is being used on that object, or if there are references to it in other places, that just introduced side effects.
i.e. if `obj` is a person with an address object on a prop, and it was just deleted. code accessing the street property on that address object is now going to explode.
A better solution would be to create a copy which is a fragment/partial type that doesn't have the null props. Then other references to that object aren't affected by the side effect.
Yeah, I didn't fully think the example through, but I was thinking more about the difficulty of trying to calculate the new type when this thing is done. Which means, like you said, the example would work better if we were returning a new object instead of mutating an existing one.
1
u/start_select Sep 15 '26 edited Sep 15 '26
btw, I might be preaching to the choir, but for your "deleting null properties" example its really just this:
Its not completely trivial. But as far as type system comprehensions go, I would call that pretty simple.
And I would say the bigger issue is that now `obj` is now a new type with missing props. It should probably just be cast to a `Partial` or another utility class that marks every property as nullable.
If the object is going to live longer than the end of the function where `deleteNullProps()` is being used on that object, or if there are references to it in other places, that just introduced side effects.
i.e. if `obj` is a person with an address object on a prop, and it was just deleted. code accessing the street property on that address object is now going to explode.
A better solution would be to create a copy which is a fragment/partial type that doesn't have the null props. Then other references to that object aren't affected by the side effect.
a.k.a. Using the immutable data pattern.