no-chris / chord-symbol

The definitive chord symbol parser and renderer for Javascript/NodeJS.
https://chord-symbol.netlify.app
MIT License
151 stars 8 forks source link

The chord renderer factory should change the type of the returned function based on the `printer` option #642

Open darrylnoakes opened 1 year ago

darrylnoakes commented 1 year ago

Currently, the renderer function returned by chordRendererFactory is always of the type (chord: Chord) => string. However, if printer: "raw" is given as an option to the factory, the returned function actually returns a Chord.

Function overloads work for fixing it, by discriminating on the type of the options object. Example:

type PrinterRaw = { printer: "raw" }

// The order is important: the first matching overload is used, hence the more specific one has to come first.
declare function chordRendererFactory(configuration: RendererConfiguration & PrinterRaw): (chord: CSChord) => CSChord
declare function chordRendererFactory(configuration?: RendererConfiguration): (chord: CSChord) => string

By the source code, I believe this issue exists with JSDoc-supplied types as well.