JavaScript
x
17
17
1
async fetchDetail(token: string): Promise < object > {
2
3
const headersRequest = {
4
Authorization: `Basic ${token}`,
5
'Content-Type': 'application/json',
6
}
7
8
return await this.httpService.get( < URL > , {
9
headers: headersRequest
10
})
11
.toPromise()
12
.then((response): object => response.data)
13
.catch(() => {
14
throw new NotFoundException()
15
})
16
}
17
I keep getting a lint issue for this line .then((response): object => response.data)
which states Unsafe use of expression of type ‘any’
Advertisement
Answer
I suspect that it’s because response
is a “generic object” and typescript can’t “identify” that it has a .data
attribute.
In order to fix that we can declare an interface of a type:
JavaScript
1
2
1
type hasData = { data: any };
2
and then use it to “explain” to TS that we expect the response to contain that attribute:
JavaScript
1
2
1
.then((response: hasData): object => response.data)
2