Currently, I am in the process of creating type definitions for a library called fessonia. This task is not new to me, but this particular library has a different organization compared to others I have worked with before, presenting a unique challenge.
The main function in this library's index.js
file is concise:
const getFessonia = (opts = {}) => {
require('./lib/util/config')(opts);
const Fessonia = {
FFmpegCommand: require('./lib/ffmpeg_command'),
FFmpegInput: require('./lib/ffmpeg_input'),
FFmpegOutput: require('./lib/ffmpeg_output'),
FilterNode: require('./lib/filter_node'),
FilterChain: require('./lib/filter_chain')
};
return Fessonia;
}
module.exports = getFessonia;
This function exports an object containing classes, with getFessonia
being the only public interface according to the library documentation. Despite other classes being accessible directly, it is recommended to only use getFessonia
due to potential configuration issues.
- I aim for the type definitions to advocate best practices when utilizing this library. This means excluding methods flagged as
@private
or not intended for external usage. For instance, proper types should enable downstream developers to declare variables with specific types:
import getFessonia from '@tedconf/fessonia';
// defining types
const config: getFessonia.ConfigOpts = {
debug: true,
};
const { FFmpegCommand, FFmpegInput, FFmpegOutput } = getFessonia(config);
- It is essential for declaration files to align with the library structure. As per the official recommendation.
My current approach involves creating separate .d.ts
files for each required .js
file to generate coherent type definitions. These files are then consolidated into index.d.ts
, enhancing clarity and usability for developers.
As I progress, some concerns arise:
- Do I need to provide a definition for the
getConfig
function, considering its limitation on direct usage? Should I export theConfig
interface directly instead and re-export it fromindex.d.ts
? Similarly, how do I manage excessive aliasing under thegetFessonia
namespace? - Re-exporting within namespaces can be tedious and less elegant.
- The potential nesting under
getFessonia
could lead to complexity in defining arguments and class shapes. - The organizational structure, such as using the
FFmpeg
namespace, might need refinement.
Your commitment to reading until the end is appreciated! While there may not be a definitive solution, I am open to exploring different approaches taken by others and welcome recommendations for improvement. Thank you!