I'm a little unsure of my TypeScript knowledge. I have a class MyClass with a member variable that is a function, but I don't know what this function will be at compile time. I want to allow external code to set this function dynamically during runtime so it can be called later. Can anyone provide the syntax in TypeScript for initializing the function as null/void/undefined and then checking if it's defined before calling it?
Here's my current code:
import { User } from 'User':
export type UserCallback = (user: User | null) => void;
export class MyClass {
private userInfoChangedCallback : UserCallback | null = null; // Initializing to null
setCallback(callback: UserCallback) {
this.userInfoChangedCallback = callback;
}
register = async(username: string, displayName: string, password: string) => {
// Some operations here...
if (this.userInfoChangedCallback === null) { // Checking if it's null
this.userInfoChangedCallback(new User(username,displayName));
}
}