An error keeps popping up at this specific line:
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'ICourseContent'. No index signature with a parameter of type 'string' was found on type 'ICourseContent'.
let elementsRemaining = 0;
[
CourseContentElementType.AUDIO,
CourseContentElementType.VIDEO,
CourseContentElementType.IMAGE,
CourseContentElementType.DOCUMENT,
CourseContentElementType.CODE,
CourseContentElementType.QUIZ,
CourseContentElementType.EMBED,
].forEach((elementType) => {
if (
// The issue is here
courseContents[this.getCourseContentIndex(courseContents, selectedCourseContentUid) - 1][
elementType as string
] != null
) {
elementsRemaining++;
}
});
The object structure is detailed below:
export interface ICourseContent {
id: number;
uid?: string;
text: ICourseContentElementText;
button: ICourseContentElementButton[];
audio?: ICourseContentElementMedia;
embed?: ICourseContentElementEmbed;
video?: ICourseContentElementMedia;
image?: ICourseContentElementImage;
quiz?: ICourseContentElementQuiz;
code?: ICourseContentElementCode;
document?: ICourseContentElementDocuments;
camera?: ICourseContentElementCamera;
}
This is the enum in use:
export enum CourseContentElementType {
CARD = 'card',
TEXT = 'text',
BUTTON = 'button',
CODE = 'code',
DOCUMENT = 'document',
AUDIO = 'audio',
VIDEO = 'video',
IMAGE = 'image',
QUIZ = 'quiz',
EMBED = 'embed',
}
How can I set up the index signature for ICourseContent to easily access content by using the enum?
I attempted the following approach:
export interface ICourseContent {
id: number;
uid?: string;
[key: string]: string;
text: ICourseContentElementText;
button: ICourseContentElementButton[];
audio?: ICourseContentElementMedia;
embed?: ICourseContentElementEmbed;
video?: ICourseContentElementMedia;
image?: ICourseContentElementImage;
quiz?: ICourseContentElementQuiz;
code?: ICourseContentElementCode;
document?: ICourseContentElementDocuments;
camera?: ICourseContentElementCamera;
}
However, it resulted in the following errors:
TS2411: Property 'id' of type 'number' is not assignable to string index type 'string'.
TS2411: Property 'uid' of type 'string | undefined' is not assignable to string index type 'string'.
TS2411: Property 'text' of type 'ICourseContentElementText' is not assignable to string index type 'string'.
TS2411: Property 'button' of type 'ICourseContentElementButton[]' is not assignable to string index type 'string'.