我正在尝试使用打字稿将接口实现到类中。
这是我的课:
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 方法中显示了这个错误:
'ArticleMySQLRepository' 类型中的属性 'find' 不能分配给基本类型 'ArticleRepository' 中的相同属性。输入 '(id: Number) => Promise<Article | null>' 不可分配给类型 '{ (id: Number): Promise<Article | 空>; (id: Number): 承诺;}'。输入'承诺<文章 | null>' 不能分配给类型 'Promise'。类型'文章| null' 不能分配给类型 'void'。类型“null”不能分配给类型“void”.t
我需要在我的 tsconfig 上更改任何设置吗?