-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add type definitions #21
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting this started @TaylorBeeston!
@ChristianMurphy good catch! I was just following the types listed in the Readme. I went ahead and added an HTMLValues type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @TaylorBeeston! 🙇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🦠 👍
I went ahead and copied over the Readme descriptions as doc comments! Should be good to go! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @TaylorBeeston! 🙇
Sweet, thanks @TaylorBeeston. Landed in 3.0.0! |
Thanks @TaylorBeeston for putting this together. I started using these types and I think I’d rather have looser types for I know I’m half a year late to the party, but can I interest you in a pull request to make this happen? This is what I had to end up writing: import rehypeSanitize from "rehype-sanitize";
import hastUtilSanitize from "hast-util-sanitize";
import hastUtilSanitizeGitHubSchema from "hast-util-sanitize/lib/github.json";
import deepMerge from "deepmerge";
// ...
.use(
rehypeSanitize,
(deepMerge(hastUtilSanitizeGitHubSchema, {
attributes: {
code: ["className"],
span: [["className", "math-inline"]],
div: [["className", "math-display"]],
},
}) as unknown) as hastUtilSanitize.Schema
) I wish to get rid of the |
I don't think the types should be loosened, typescript should be strongly typed by default, and people can opt out with If you're interested in ensuring |
Thanks for the detailed explanation! I’m happy that we don’t need the I tried The following worked: .use(
rehypeSanitize,
deepMerge<hastUtilSanitize.Schema>(hastUtilSanitizeGitHubSchema as any, {
attributes: {
code: ["className"],
span: [["className", "math-inline"]],
div: [["className", "math-display"]],
},
})
) It’s already better than what I had before. If I understand this correctly the next step to get rid of the |
Some packages, like https://github.com/sindresorhus/spdx-license-list will mirror json files with js files that can be more easily typed. |
Thank you for your help 😃
Thanks, I’ll check that out…
I’m not sure I understand what you mean. With "attributes": {
a: string[];
img: string[];
input: (string | boolean)[][];
li: string[][];
div: string[];
blockquote: string[];
del: string[];
ins: string[];
q: string[];
"*": string[];
} I think the type should be something like Does that make sense? |
That could be, a codesandbox would be helpful
Too specific, as in incompatible?
? If so there is a test case in the type tests, which I believe, covers this case, which is passing: hast-util-sanitize/types/hast-util-sanitize-tests.ts Lines 194 to 197 in 4dd66cc
Again, a code sandbox would be helpful for tracing the issue. |
Here’s a repository showing what I mean: https://github.com/leafac/hast-util-sanitize--21 I’d like to provide you with a CodeSandbox, but it appears that CodeSandbox may be misbehaving. I mean, there’s an issue in the path in the CodeSandbox you provided ( In any case, here’s the error I’m referring to when you use
This error message is a bit nightmarish, but I believe it’s saying that we’re both correct: both the |
Right, and I see that as a limitation of TypeScript, one which is documented and being discussed in microsoft/TypeScript#32063. |
👍 Got it. Thanks for the information.
I agree.
I’ll look into that… |
Okay, so I looked more into the issue and I think that the best course of action is to not bother with types for the JSON, because the whole notion of loading JSON with My conclusion: All these hoops you have to jump through to get |
We can’t do that because fs is not available in browsers: but you can! |
Closes GH-20
.This PR adds the
types
directory that contains anindex.d.ts
file that allows TypeScript projects to use this package more easily. Additionally, there are some tests to ensure the correctness of this type definition, and some boilerplate associated withdtslint
.package.json
has been updated to includedtslint
as a dev dependency. There is also the addition of the test-types script to run dtslint, and index.d.ts is exported as the types file for this project.One thing I would like to note is that if you currently tried to use
lib/github.json
as is written in the Readme, TypeScript will make your life difficult due to theinput
attribute
. The reason for this is because I tried to type theattributes
object as strongly as I could by using the type{[key: string]: Array<string | [string, ...any[]]>}
. This is canonically correct, allowing shapes such asWhich is how I interpreted
from the Readme.
However, TS will infer the type
Array<Array<string | boolean>>
forattributes
from the JSON object and decide that the two types are incompatible. To defeat this in the tests, I simply inlined the Github JSON and explicitly defined it as the correct type. If it was needed to be used, you'd have to type outWhich is pretty ugly in my opinion.
A solution to this could be to loosen the
attributes
type definition, but that comes with its own set of problems.