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

Can the parse function be typed to return a string by default? #3432

Closed
mirkonasato opened this issue Aug 27, 2024 · 3 comments
Closed

Can the parse function be typed to return a string by default? #3432

mirkonasato opened this issue Aug 27, 2024 · 3 comments
Labels

Comments

@mirkonasato
Copy link

mirkonasato commented Aug 27, 2024

What pain point are you perceiving?.

I see that after change #3341 released in v14.0.0 the value returned by the parse function is correctly inferred to be a string in TypeScript, but only if you explicitly pass async: false

const html: string = marked.parse(md, { async: false });

However it's still a Promise<string> | string by default

const html: Promise<string> | string = marked.parse(md);

forcing me to cast the result, which is not ideal:

const html = marked.parse(md) as string;

Describe the solution you'd like

Is there any reason why parse cannot treat the case where no options are passed in the same way as when passing async: false? So users can write

const html: string = marked.parse(md);

It seems to me the overloadedParse type could be simplified to

    type overloadedParse = {
      (src: string, options: MarkedOptions & { async: true }): Promise<string>;
      (src: string, options?: MarkedOptions | undefined | null): string;
    };

given that TypeScript will pick the first matching overload.

Happy to put together a PR if this makes sense, but I first wanted to check if I'm missing something because I'm not familiar with the full codebase.

@UziTech
Copy link
Member

UziTech commented Aug 27, 2024

If an extension sets { async: true } then marked.parse(md) will return a Promise<string>

import { marked } from 'marked';

marked.use({ async: true });

marked.parse(''); // will return Promise<string>

@mirkonasato
Copy link
Author

Understood, thanks.

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

No branches or pull requests

3 participants
@UziTech @mirkonasato and others