-
Notifications
You must be signed in to change notification settings - Fork 73
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 incorrect type-declaration #106
Conversation
For everyone facing issues with the type-declarations: {
"paths": {
"markdown-it-anchor": [
"./node_modules/@types/markdown-it-anchor"
]
}
} This will make typescript use the type-declarations found at |
Awesome! Thanks a ton for taking care |
Thank you so much for your PR! I don't know TypeScript very well, before I publish it, can you let me know if changing Also I'm wondering, would it make sense to move Cheers! |
Edit: ok it seems when using microbundle to derive both CommonJS and UMD dist files, they use Old commentAlso now I'm thinking about it, the actual JS code does More specifically this module uses the following pattern in "main": "dist/markdownItAnchor.js",
"module": "dist/markdownItAnchor.mjs", I'm not sure what would be the proper way with TypeScript to handle that, I believe both |
Sadly, I don't know what the best practice is. I'll try to make up some simple examples. Let's say I create a package which prints error-messages using import Markdown = require("markdown-it");
try {
doWork();
} catch (error) {
return new MarkdownIt.render(
dedent(`
# An Error Occurred
~~~
${error}
~~~`));
} In this case, no types from But as soon as you expose types from your A few examples: import MarkdownIt = require("markdown-it");
/* Exposing types by extending a class */
export class AwesomeMarkdownIt extends MarkdownIt { }
/* Exposing types by accepting parameters */
export class Converter {
public Convert(parser: MarkdownIt, md: string) {
return parser.render(string);
}
}
/* Exposing types through properties and fields */
export class Converter {
public get Parser(): MarkdownIt {
return null;
}
}
// or
export class Converter {
public Parser: MarkdownIt = null;
} In these cases, types from |
Ah that makes sense, while the runtime doesn't need the types, putting them in I'll move |
I just had a look at an example esm/mjs module here: https://github.com/gfmio/typescript-esm-cjs-hybrid-example Looks like you have to use I cloned said repository and changed the Trying to compile this project by running
|
Sounds great, thanks for your effort 😄 |
I found a solution here: developit/microbundle#712 Thanks a lot for being patient |
Oh interesting. I also found that I could get TypeScript to accept both syntaxes in a way that works at runtime by doing I believe that developit/microbundle#712 would be more if markdown-it-anchor used named exports, but currently since we only have a default export that's why we can do I added the |
Yeah, that works great 😄 You could even go as far and add a separate |
Cool, I just published 8.3.1 with that patch! I'll leave the |
Agreed! |
The current type-declarations of
markdown-it-anchor
are incorrect.Thus, this module can only be consumed if
esModuleInterop
is set totrue
in the project which tries to consumemarkdown-it-anchor
.This PR changes the type-declarations properly.
All,
markdown-it
,markdown-it/lib/token
andmarkdown-it/lib/rules_core/state_core
use amodule.exports = [...]
-statement rather than aexport default [,,,]
-statement.However, as seen here, the imports are written as if a
export default [...]
-module is being imported:markdown-it-anchor/types/index.d.ts
Lines 1 to 3 in 5592824
Therefore, these import-statements need to be changed to
import x = require("module-x");
.What's more, the
markdown-it-anchor
-module uses anexport default x
-statement rather than amodule.exports = x
-statement.However, the type-declaration suggests that the module is a
module.exports = x
-module:markdown-it-anchor/types/index.d.ts
Line 65 in 5592824
In order to fix this, I changed this statement to
export default anchor;
.Also I added
@types/markdown-it
to thedependencies
as the type-declarations depend on it.Optionally you could add it to
peerDependencies
but I'm not quite sure whether that's the right way to got, tbh.I'd love to see this implemented in a new patch-version as soon as possible.
Please note, that this change was taken from the previous type-declaration available on
DefinitelyTyped
:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/9c1e78c99106fc3e1be574ca1b968d01963e032d/types/markdown-it-anchor/index.d.ts