At the moment, it is feasible to have a setter that supports both undefined
/null
and a getter for non-undefined
/non-null
values:
export interface IProperty<T> {
get value(): T;
set value(value: T | undefined);
}
// the following is ok with "strictNullChecks": true
const a: IProperty<string> = {value: ''};
a.value = undefined;
const b = a.value;
console.log(b);
I am looking to create a generic type that allows setting undefined for all properties of an object, as indicated in the comment:
// this type works, but it also makes getter nullable :(
export type EditableObject<T> = {
[K in keyof T]: EditableObject<T[K]> | undefined;
};
This may not look ideal, but such a type is necessary to begin utilizing strictNullChecks
in a larger application.
export function editableScope<T>(item: T, block: (item: EditableObject<T>) => void): void {
block(item);
}