Tests if the given property of an object is available, and if so passes the property value to the given
callback.
Remarks: A possible bug in TypeScript is that even though we null check the property value before passing it
to the callback, if TypeScript deduces the type of the property to be in the domain of null | undefined, that
such constraints still flow through to the callback function parameter.
Consider this interface:
example
interface Foo {
name: string;
bar: () => void | undefined;
}
let o: Foo = ...;
safePropAccess(o, "bar", func => {
//TypeScript still thinks func is [() => void | undefined] even though safePropGet tests for null/undefined
//before passing. To workaround this, use the ! operator to override and tell TypeScript that "func" cannot
//possibly be null or undefined
func!();
});
Tests if the given property of an object is available, and if so passes the property value to the given callback.
Remarks: A possible bug in TypeScript is that even though we null check the property value before passing it to the callback, if TypeScript deduces the type of the property to be in the domain of null | undefined, that such constraints still flow through to the callback function parameter.
Consider this interface:
interface Foo { name: string; bar: () => void | undefined; } let o: Foo = ...; safePropAccess(o, "bar", func => { //TypeScript still thinks func is [() => void | undefined] even though safePropGet tests for null/undefined //before passing. To workaround this, use the ! operator to override and tell TypeScript that "func" cannot //possibly be null or undefined func!(); });
0.11