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

fix: fix marked types #3103

Merged
merged 1 commit into from Nov 29, 2023
Merged

fix: fix marked types #3103

merged 1 commit into from Nov 29, 2023

Conversation

UziTech
Copy link
Member

@UziTech UziTech commented Nov 22, 2023

Marked version: 10.0.0

Description

Fix marked and marked.parse types. async: false could still return a Promise<string> if an extension sets async: true.

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

Copy link

vercel bot commented Nov 22, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
marked-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2023 5:17am

@UziTech UziTech merged commit edae309 into markedjs:master Nov 29, 2023
8 checks passed
github-actions bot pushed a commit that referenced this pull request Nov 29, 2023
# [11.0.0](v10.0.0...v11.0.0) (2023-11-29)

### Bug Fixes

* clean up rules so they can be typed ([#3087](#3087)) ([175fc0c](175fc0c))
* fix marked types ([#3103](#3103)) ([edae309](edae309))

### BREAKING CHANGES

* Lexer.rules object has been changed so it can be properly types. Some intermediate rules have been removed.
@crystalfp
Copy link

Well, with version 11 I have to cast the result to string to have it works:

marked.parse(markdown, {gfm: true, async: false}) as string).replaceAll(...)

Never needed before.

@UziTech
Copy link
Member Author

UziTech commented Nov 29, 2023

Yes but it is actually correctly typed now. Before it would be typed as a string even though it could return a Promise.

It would be great if typescript provided a way to return the type based on marked.defaults.async. But as far as I know typescript doesn't have a feature to do that.

@crystalfp
Copy link

Ah, this explain everything and I'm happy with the cast solution.
Other possibility, instead of relying on "async" option, is to provide two different entry points like fast-glob does: fglob(): Promise<string[]> and fglob.sync(): string[].
Anyway, thanks for clarifying the issue.
mario

@UziTech UziTech deleted the async-promise branch November 29, 2023 07:58
DafyddLlyr added a commit to theopensystemslab/planx-core that referenced this pull request Dec 4, 2023
Replaces #213 

Please see this issue for context:
markedjs/marked#3103
@SupremeVoid
Copy link

SupremeVoid commented Jan 18, 2024

Yes but it is actually correctly typed now. Before it would be typed as a string even though it could return a Promise.

It would be great if typescript provided a way to return the type based on marked.defaults.async. But as far as I know typescript doesn't have a feature to do that.

How about using a conditional type?

type ParseType<T> = T extends { async: true } ? Promise<string> : string;

function parse<T extends MarkedOptions>(src: string, options?: T): ParseType<T> {}

const string = parse('SomeSrc'); // this returns a string
const string2 = parse('SomeSrc', {async: false});  // this still returns a string
const string3 = parse('SomeSrc', {async: true});  // this returns a Promise<string>

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

Successfully merging this pull request may close these issues.

marked.parseInline() returns string | Promise<string> even when { async: false }
3 participants