My attempt at coding a class in TypeScript has hit a roadblock. Whenever I try to utilize Jsons in an Array, the system crashes due to a TypeScript error. Here's the class I tried to create:
class Api {
url: string | undefined;
credentials: Array<JSON> = [];
headers: Array<JSON> = [];
send(url: string, credentials: Array<JSON>, headers: Array<JSON>) {
this.url = url;
this.credentials = credentials;
this.headers = headers;
}
bearer(){
this.headers.push({
"Authorization" : "Bearer "
})
}
}
module.exports = new Api()
Unfortunately, when attempting to set the bearer
headers, an error is encountered:
Argument of type '{ Authorization: string; }' is not assignable to parameter of type 'JSON'.
Object literal may only specify known properties, and '"Authorization"' does not exist in type 'JSON'.
I have tried using interfaces as a solution, but that did not work either. This was my initial interface attempt:
interface credentialType {
[x: string]: string
}
However, this approach also proved ineffective.
Following that, I made another attempt with a revised interface structure:
interface credentialType {
type: string;
value: string
}
...
this.header.push({
type: 'Authen',
value: 'Bearer'
})
Despite these efforts, the issue still persists.