typescriptrepository-patternts-nodets-node-dev

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


I am trying to implement an interface into a class using typescript.

This is my 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]
      )
  }
}

Then my interface:

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

I need to follow that interface for any other repository I want to implement. By the way, the editor is showing this error in the find method:

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

Is there any setting I need to change on my tsconfig?


Solution

  • Ah, I didn't spot it at first, but your interface defines find() twice:

    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
    }
    

    You probably want to get rid of the last one.