I have the problem, that I have a function that gets an object. A field of this object can be null, which I want to handle, and afterwards add a field to this object and return it.
Here you can find a simplified version of it.
The problem that I have is that the assertion that the field is not null is lost after I create the spread clone.
Is there a way to get around this?
>Solution :
The bar.field is string[] information is kept only in bar.field, not in bar itself
type Type1 = {
field: string[] | null;
}
type Type2 = {
name: string;
field: string[]
}
function foo(bar: Type1): Type2 {
bar.field ??= [];
const result = { name: "some Name", ...bar, field: bar.field };
result.field.push("Some String");
return result;
}