Skip to content

Commit

Permalink
Fix addScheme() typings
Browse files Browse the repository at this point in the history
One type of argument was missing, as `scheme` can have 2 shapes (either a class derived from Colormaker or a function that redefines the Colormaker prototype on a derived class).
It was necessary to add type predicates to correctly infer the type of `scheme` within the addScheme method.
  • Loading branch information
ppillot committed Jan 9, 2024
1 parent 5e6cf7c commit 9554f04
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/color/colormaker-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Colormaker, { ColormakerConstructor, ColormakerParameters } from './color
import SelectionColormaker, { SelectionSchemeData } from './selection-colormaker'
import Structure from '../structure/structure'

type ColormakerDefinitionFunction = ((this: Colormaker, param?: ColormakerParameters) => void)

const ColormakerScales = {
'': '',

Expand Down Expand Up @@ -170,8 +172,8 @@ class ColormakerRegistry {
* @param {String} label - scheme label
* @return {String} id to refer to the registered scheme
*/
addScheme (scheme: ColormakerConstructor, label?: string) {
if (!(scheme instanceof Colormaker)) {
addScheme (scheme: ColormakerConstructor|ColormakerDefinitionFunction, label?: string) {
if (!(isColormakerSubClass(scheme))) {
scheme = this._createScheme(scheme)
}

Expand Down Expand Up @@ -202,7 +204,7 @@ class ColormakerRegistry {
delete this.userSchemes[ id ]
}

_createScheme (constructor: any): ColormakerConstructor {
_createScheme (constructor: ColormakerDefinitionFunction): ColormakerConstructor {
class _Colormaker extends Colormaker {
constructor (params: ColormakerParameters) {
super(params)
Expand Down Expand Up @@ -256,4 +258,10 @@ class ColormakerRegistry {
}
}

function isColormakerSubClass (
scheme: ColormakerConstructor|((this: Colormaker, param?: ColormakerParameters) => void)
): scheme is ColormakerConstructor {
return (scheme instanceof Colormaker)
}

export default ColormakerRegistry

0 comments on commit 9554f04

Please sign in to comment.