I am facing an issue with a foundational type in my code:
export abstract class ElementBase extends HTMLElement {
private state: StateBase;
private propertyToWatch: string;
update() {
const externalValue = this.state[this.propertyToWatch]; // <- this doesn't compile
// Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'StateBase'.
this.updateUI(externalValue);
}
...
The class StateBase
has two methods and is inherited by various other classes that define properties like pageNumber: number;
. I am unable to access the subclass properties using named indexing due to TypeScript v5.x deprecating the suppressImplicitAnyIndexErrors
configuration switch. According to this guidance:
Fix: Add an index signature to the relevant type (or use a type assertion at the indexing location).
However, I am struggling to implement this for subtypes that are not known at runtime. If I add an index signature to StateBase
, it disrupts the methods and properties of both the base type and its inheritors. Using a type assertion also seems impractical when dealing with subtypes. What could I be overlooking?