I'm struggling to properly type my Twitter widget in TypeScript.
Currently, I am encountering several errors such as:
ESLint: Unsafe call of an any typed value.(@typescript-eslint/no-unsafe-call)
ESLint: Unsafe member access .catch on an any value.(@typescript-eslint/no-unsafe-member-access)
ESLint: Unsafe member access .createTweet on an any value.(@typescript-eslint/no-unsafe-member-access)
ESLint: Unsafe member access .finally on an any value.(@typescript-eslint/no-unsafe-member-access)
ESLint: Unsafe member access .then on an any value.(@typescript-eslint/no-unsafe-member-access)
I want to resolve this by defining an interface for the Twitter widget but I'm uncertain about how to accomplish that.
loadTwitterWidget(): void {
this.setLoadingStatus.emit(true);
this.courseContentElementEmbedTweetService
.loadScript()
.pipe(take(1))
.subscribe(
// Planning to create an interface here
(twitter) => {
(this.tweet.nativeElement as HTMLElement).innerHTML = null;
// The error arises at this point
twitter['widgets']
.createTweet(this.courseContentElementEmbed.id, this.tweet.nativeElement, {
...this.tweetOptions,
})
.then((r) => {
if (!r) {
this.setErrorStatus.next();
} else {
this.setSuccessStatus.next();
}
})
.catch((error) => this.logger.error(error))
.finally(() => this.setLoadingStatus.emit(false));
},
(error) => this.logger.error(error)
);
}
So far, I have attempted the following solution:
export interface ICourseContentElementEmbedTweetWidget {
widgets: {
createTweet: unknown
}
[key: string]: string;
}
However, I encounter the error
TS2411: Property 'widgets' of type '{ createTweet: unknown; }' is not assignable to string index type '
.