【发布时间】:2022-01-03 05:27:20
【问题描述】:
我一直在学习课程和 TypeScript,遇到了一个我不明白的错误 - 希望有人可以帮助我。
我已经创建了如下界面:
export default interface IServerApi {
url: string
call: string
}
然后我创建了一个应该使我的 apicalls 成为这样的类:
class ApiCall implements IServerApi {
public url: string
public call: string
constructor(apiUrl: string, apiEndpoint: string) {
this.url = process.env.REACT_APP_SERVER_URL as string
this.call = "products"
}
static async getAll() {
const response = await fetch(`${this.url}/${this.call}`)
const data = await response.json()
return data
}
}
但是,我在 this.url 下面收到一个 TS 错误,并显示以下消息:
any
Property 'url' does not exist on type 'typeof ApiCall'.ts(2339)
谁能指出这里发生了什么?我认为 this.url 会引用构造函数中设置的 url,然后我可以将类似 this.url 的内容传递给方法。目标是能够调用 ApiCall.getAll() 而不是重写 fetch。
谢谢!
【问题讨论】:
-
getAll是静态的,url在实例上。 -
是时候做更多关于静态的阅读了。谢谢!
标签: javascript reactjs typescript