类型 'Article | null' 不可分配给类型 'void'

Type 'Article | null' is not assignable to type 'void'

我正在尝试使用打字稿将接口实现到 class。

这是我的 class:

import connector from '../../../../common/mysql.persistence'
import { Article } from '../../domain/article'
import { ArticleRepository } from '../../article.repository'

export class ArticleMySQLRepository implements ArticleRepository {
  public async all(): Promise<Article[]> {
    const [rows] = await connector.execute(
      'SELECT * FROM articles ORDER BY id DESC'
    )

    return rows as Article[]
  }

  public async find(id: Number): Promise<Article | null> {
    const [rows]: any[] = await connector.execute(
      'SELECT * FROM articles WHERE id = ?',
      [id]
    )

    if (rows.length) {
      return rows[0] as Article
    }

    return null
  }

  public async store(entry: Article): Promise<void> {
    const date = new Date()
    const likes:number = 0
    const shares:number = 0
    
    await connector.execute(
      'INSERT INTO article(id, title, slug, description, content, likes, shares, updatedAt, createdAt) VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
      [entry.id, entry.title, entry.slug, entry.description, entry.content, likes, shares, null, date ]
    )
  }

  public async update(entry: Article): Promise<void> {
    const date = new Date()

    await connector.execute(
      'UPDATE article SET title = ?, slug = ?, description = ?, content = ?, updatedAt = ? WHERE id = ?',
      [entry.title, entry.slug, entry.description, entry.content, date, entry.id]
    )
  }

  public async remove (id: Number): Promise<void> {
    await connector.execute(
        'DELETE FROM article WHERE id = ?',
        [id]
      )
  }
}

然后我的界面:

import { Article } from './domain/article'

export interface ArticleRepository {
  all(): Promise<Article[]>
  find(id: Number):Promise<Article | null>
  store(entry: Article):Promise<void>
  update(entry: Article):Promise<void>
  find(id: Number): Promise<void>
}

对于我想要实现的任何其他存储库,我需要遵循该接口。顺便说一下,编辑器在 find 方法中显示了这个错误:

Property 'find' in type 'ArticleMySQLRepository' is not assignable to the same property in base type 'ArticleRepository'. Type '(id: Number) => Promise<Article | null>' is not assignable to type '{ (id: Number): Promise<Article | null>; (id: Number): Promise; }'. Type 'Promise<Article | null>' is not assignable to type 'Promise'. Type 'Article | null' is not assignable to type 'void'. Type 'null' is not assignable to type 'void'.t

我的 tsconfig 是否需要更改任何设置?

啊,我一开始没看出来,但是你的接口定义了两次find()

export interface ArticleRepository {
  all(): Promise<Article[]>
  find(id: Number):Promise<Article | null> // defined here
  store(entry: Article):Promise<void>
  update(entry: Article):Promise<void>
  find(id: Number): Promise<void> // defined here
}

您可能想去掉最后一个。