Allow generic type parameters for custom extensions #3230
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Marked version: 11.1.0
Markdown flavor: n/a
Description
This PR adds optional generic type parameters to the types used for creating custom extensions. This allows marked to communicate more specific expectations to TypeScript for the token returned by a custom
tokenizer
function, and for the token passed to a customrenderer
function.Example usage:
Unfortunately, it's not quite so easy to set this to allow the generic type parameters to be inferred as I had hoped. I'd forgotten when writing the issue that TypeScript is only able to infer generic types in function parameters, so that can't be done with type updates alone.
Enabling this sort of type inference would require a noop function wrapper to allow TypeScript to do inference, and I'm not sure if that would be a useful addition to marked's API:
I'm also less certain about how valuable it is to include the name in the generic parameter. Particularly as it's necessary to declare
as const
if the token is not immediately returned, e.g.Because this PR only contains type changes, no actual functionality should have changed. And because the only changes have been adding generic type arguments with defaults that replicate the existing behaviour, I expect there wouldn't be any breaking changes with existing TypeScript code.
I'm not sure where best to document this feature's PR, so currently have not included any new documentation.
Contributor
Committer
In most cases, this should be a different person than the contributor.