You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What pain point are you perceiving?.
I've recently started writing custom some TokenizerAndRendererExtension code in a personal project, using TypeScript, and I've found it slightly annoying to work with the way the extension's renderer method receives a Token.Generic even though (I assume) it should be possible to know that it receives a token of the type returned by its tokenizer method.
Current solutions involve either using type assertions to tell the TypeScript compiler that I know more about the structure of these tokens than TypeScript is telling me, or validating the shape ourselves (which adds additional work to rendering that AFAIK shouldn't be necessary so isn't ideal). Both approaches require manually keeping type assertions or checks in the renderer updated whenever the shape of the token changes in the tokenizer.
On a related but less important note, it could be nice to also enforce the constraint that the extension's name string must be the same as its token's name string.
Describe the solution you'd like
Currently, marked provides a Token.Generic type for general-use tokens, which looks like this:
This allows access to any properties, but they are typed unsafely as any so it's up to the consuming code to decide how to deal with that.
I think this could be resolved by allowing TokenizerAndRendererExtension to take a generic type parameter, defining the type of token it uses internally. I believe this could be written in such a way as to allow that generic type parameter to be inferred through the value returned by the tokenizer function. The parameter could have a default type that would maintain the current any behaviour, so existing code that references this type without a generic type parameter would still work in the same way.
However, this approach would also necessarily make a few other internal types (in particular TokenizerExtension and RendererExtension) generic as well. Without having attempted to implement this, I'm not sure if there would be additional complexity spreading to other types. I don't have any knowledge currently of marked's codebase, aside perhaps from having viewed the type declaration files it ships with.
The text was updated successfully, but these errors were encountered:
What pain point are you perceiving?.
I've recently started writing custom some
TokenizerAndRendererExtension
code in a personal project, using TypeScript, and I've found it slightly annoying to work with the way the extension'srenderer
method receives aToken.Generic
even though (I assume) it should be possible to know that it receives a token of the type returned by itstokenizer
method.Current solutions involve either using type assertions to tell the TypeScript compiler that I know more about the structure of these tokens than TypeScript is telling me, or validating the shape ourselves (which adds additional work to rendering that AFAIK shouldn't be necessary so isn't ideal). Both approaches require manually keeping type assertions or checks in the
renderer
updated whenever the shape of the token changes in thetokenizer
.On a related but less important note, it could be nice to also enforce the constraint that the extension's
name
string must be the same as its token'sname
string.Describe the solution you'd like
Currently, marked provides a
Token.Generic
type for general-use tokens, which looks like this:This allows access to any properties, but they are typed unsafely as
any
so it's up to the consuming code to decide how to deal with that.I think this could be resolved by allowing
TokenizerAndRendererExtension
to take a generic type parameter, defining the type of token it uses internally. I believe this could be written in such a way as to allow that generic type parameter to be inferred through the value returned by thetokenizer
function. The parameter could have a default type that would maintain the currentany
behaviour, so existing code that references this type without a generic type parameter would still work in the same way.However, this approach would also necessarily make a few other internal types (in particular
TokenizerExtension
andRendererExtension
) generic as well. Without having attempted to implement this, I'm not sure if there would be additional complexity spreading to other types. I don't have any knowledge currently of marked's codebase, aside perhaps from having viewed the type declaration files it ships with.The text was updated successfully, but these errors were encountered: