Searching for the best practice to inform the ts compiler that a method will be generated at runtime.
interface Todo { /* ... */ }
export class TodoModel {
todos: Todo[] = [];
constructor() {
//...
}
bindTodoListChanged(callback : (todos: Todo[]) => void) {
this.onTodoListChanged = callback
}
_save(todos: Todo[]) {
this.onTodoListChanged(todos)
localStorage.setItem('todos', JSON.stringify(todos))
}
}
An error I encountered:
Property 'onTodoListChanged' does not exist on type 'TodoModel'. Did you mean 'bindTodoListChanged'?
Thought using an assert !
would fix it
bindTodoListChanged(callback : (todos: Todo[]) => void) {
this.onTodoListChanged! = callback
}
_commit(todos: Todo[]) {
this.onTodoListChanged(todos)
localStorage.setItem('todos', JSON.stringify(todos))
}
I also attempted
onTodoListChanged(todos: Todo[]): void
bindTodoListChanged(callback : (todos: Todo[]) => void) {
this.onTodoListChanged! = callback
}
_commit(todos: Todo[]) {
this.onTodoListChanged(todos)
localStorage.setItem('todos', JSON.stringify(todos))
}
Resulting in this error:
Function implementation name must be 'onTodoListChanged'.
Tried
"strictPropertyInitialization": false
in my tsconfig.json
But prefer avoiding config alterations.