Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript support (plugins) #24

Open
spencercap opened this issue Oct 20, 2021 · 1 comment
Open

typescript support (plugins) #24

spencercap opened this issue Oct 20, 2021 · 1 comment

Comments

@spencercap
Copy link

what a fun library! thanks @bryanbraun 👏

i noticed you're using pika for automatically building types. this is rad for the main checkboxland instance, but, when it comes to plugins those types are not inferred. since a plugin's js file does not export its internal functions, pika doesnt either.


for example, in print.js, if the print function is exported too

export default {
  name: 'print',
  exec: print,
  print // added this line
}

THEN, i believe the output .d.ts file would look something like:

declare namespace _default {
    export const name: string;
    export { print as exec };
    export { print as print }; // added this line
}
export default _default;
declare function print(text: any, options?: {}): any;

and THEN people using your lib in typescript could augment the main checkboxland class w plugins as needed, something like:

import { Checkboxland } from 'checkboxland';
import CblPluginPrintTypes from '../../node_modules/checkboxland/dist-types/plugins/print/print';
// augmented 
type CblPluggedIn = Checkboxland & typeof CblPluginPrintTypes;
const cbl = new Checkboxland({ dimensions }) as CblPluggedIn;
cbl.print('words'); // works correctly without ts errors

of course, there are other ways to correctly add plugin typings to the cbl class in typescript and maybe you have an idea for the builtin plugins, just offering one solution.

cheers

@bryanbraun
Copy link
Owner

Hey! Thanks for the heads up... it's a good callout.

I'd have to test this out but I think it's a good approach since the types would still be generated automatically (and that's something I'd want to preserve).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants