Unable to extend TinyColor class and chaine methods
ccpu opened this issue Β· comments
I would like to extent TinyColor
class to provide more functionality for my own needs, however its not possible to extend and chain methods because of the return types.
Here is an example of extending class:
export class MyTinyColor extends TinyColor {
css(): string {
return 'do some work and return value';
}
}
chaining 'css' method will cause compiler error (Property 'css' does not exist on type 'TinyColor'):
const color= new MyTinyColor ('#000').setAlpha(0.54).css();
The the reason is that the setAlpha
returns TinyColor
and TinyColor
has not implemented css
mehtod.
To solve it we can either not specifying return type or return this
:
setAlpha(alpha?: string | number) : this {
...
}
Not sure if there is any other way!
π This issue has been resolved in version 3.3.2 π
The release is available on:
Your semantic-release bot π¦π
setAlpha is one of the only instances where we return this
instead of a new tinycolor instance. I've released a new version so you could chain them but you will run into issues with other functions.
Recommend not adding more functions to the class but instead writing utility functions that take tinycolor and do whatever you want