如何从另一个方法创建一个方法?

huangapple go评论67阅读模式
英文:

How to create a method from another method?

问题

我正在尝试找到告诉TypeScript编译器一个方法将在运行时创建的最佳做法。

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))
  }
}

我当前收到以下错误:

Property 'onTodoListChanged' does not exist on type 'TodoModel'. Did you mean 'bindTodoListChanged'?

我认为使用断言 ! 可能有所帮助

bindTodoListChanged(callback: (todos: Todo[]) => void) {
  this.onTodoListChanged! = callback
}

_commit(todos: Todo[]) {
  this.onTodoListChanged(todos)
  localStorage.setItem('todos', JSON.stringify(todos))
}

我还尝试过

onTodoListChanged(todos: Todo[]): void

bindTodoListChanged(callback: (todos: Todo[]) => void) {
  this.onTodoListChanged! = callback
}

_commit(todos: Todo[]) {
  this.onTodoListChanged(todos)
  localStorage.setItem('todos', JSON.stringify(todos))
}

这产生了以下错误:

Function implementation name must be 'onTodoListChanged'.

最后,我尝试在我的 tsconfig.json 中使用 "strictPropertyInitialization": false,但我更喜欢不必更改配置。

英文:

I'm trying to to find the best practice for telling the ts compiler that a method will be created at run time.


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))
 }

}

I currently receive this error:

 Property 'onTodoListChanged' does not exist on type 'TodoModel'. Did you mean 'bindTodoListChanged'?

I thought using an assert ! would help

 bindTodoListChanged(callback : (todos: Todo[]) => void) {
   this.onTodoListChanged! = callback
 }

 _commit(todos: Todo[]) {
   this.onTodoListChanged(todos)
   localStorage.setItem('todos', JSON.stringify(todos))
 }

I've also tried


onTodoListChanged(todos: Todo[]): void

 bindTodoListChanged(callback : (todos: Todo[]) => void) {
   this.onTodoListChanged! = callback
 }

 _commit(todos: Todo[]) {
   this.onTodoListChanged(todos)
   localStorage.setItem('todos', JSON.stringify(todos))
 }

Which produced this error:

Function implementation name must be 'onTodoListChanged'.

Lastly, I've tried "strictPropertyInitialization": false in my tsconfig.json But I would prefer to not have to alter the config.

答案1

得分: 3

你需要声明 class field onTodoListChanged,可能是一个 optional property,因为它可能不存在:

class TodoModel {
    /* ⋯ */
    onTodoListChanged?: (todos: Todo[]) => void
    /* ⋯ */
}

这将阻止编译器抱怨 onTodoListChanged 不存在:

bindTodoListChanged(callback: (todos: Todo[]) => void) {
    this.onTodoListChanged = callback // okay
}

因为它是可选的,你需要在调用它之前检查它是否未定义;最简单的方法可能是使用 optional chaining operator (?.)

_save(todos: Todo[]) {
    this.onTodoListChanged?.(todos) // okay
    localStorage.setItem('todos', JSON.stringify(todos))
}

Playground代码链接

英文:

You need to declare the class field onTodoListChanged, presumably as an optional property because it might not exist:

class TodoModel {
    /* ⋯ */
    onTodoListChanged?: (todos: Todo[]) => void
    /* ⋯ */
}    

This will stop the compiler from complaining that onTodoListChanged doesn't exist:

bindTodoListChanged(callback: (todos: Todo[]) => void) {
    this.onTodoListChanged = callback // okay
}

Because it's optional, you'll have to check that it's not undefined before calling it; the easiest way to do that is probably to use the optional chaining operator (?.):

_save(todos: Todo[]) {
    this.onTodoListChanged?.(todos) // okay
    localStorage.setItem('todos', JSON.stringify(todos))
}

Playground link to code

huangapple
  • 本文由 发表于 2023年4月11日 03:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定