When trying to upgrade from ESLint 4.x to ESLint 7.x, it’s no longer allowed to merge interfaces and classes and so I get the linting error Unsafe declaration merging between classes and interfaces.
This is the current implementation with the error:
interface ClassNode {
'outputclass'?: string
'class'?: string
}
interface LocalizationNode {
'dir'?: string
'xml:lang'?: string
'translate'?: string
}
interface AltNode extends LocalizationNode, ClassNode {
'text': string
}
abstract class BaseNode { }
class AltNode extends BaseNode { }
Renaming the interfaces by themselves so that their names do not clash with the class names is not enough, as the following code:
interface ClassNodeAttributes {
'outputclass'?: string
'class'?: string
}
interface LocalizationNodeAttributes {
'dir'?: string
'xml:lang'?: string
'translate'?: string
}
interface AltNodeAttributes extends LocalizationNodeAttributes, ClassNodeAttributes {
'text': string
}
abstract class BaseNode { }
class AltNode extends BaseNode implements AltNodeAttributes { }
Now I get following error message:
2420: Class 'AltNode' incorrectly implements interface 'AltNodeAttributes'.
Property ''text'' is missing in type 'BaseNode' but required in type 'AltNodeAttributes'.
It seems that class AltNode is incorrectly implementing interface AltNodeAttributes, because property text is missing in type AltNode
One solution to fixing the compilation errors appears to be for me to duplicate all of the attributes from the interfaces into the class, for example:
class AltNode extends BaseNode implements AltNodeAttributes {
'outputclass'?: string
'class'?: string
'dir'?: string
'xml:lang'?: string
'translate'?: string
'text': string
}
However this approach doesn't seem optimal to me as it violates the DRY principle because I have had to define all of the attributes again, and additionally they are no longer gathered from their interfaces.