Skip to content

Commit

Permalink
README: add remarks about changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manuth committed Jun 29, 2022
1 parent fc22ad6 commit 58d8224
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ interface Location {
}
```
### Query comments in TypeScript
`comment-json` provides a `symbol`-type called `CommentSymbol` which can be used for querying comments.
Furthermore, a type `CommentDescriptor` is provided for enforcing properly formatted symbol names:
```ts
import { CommentDescriptor, CommentSymbol, parse } from "comment-json"

const parsed = parse(`{ /* test */ "foo": "bar" }`)
const symbolName: CommentDescriptor: "before-prop:foo"; // typescript only allows properly formatted symbol names here
console.log(parsed[Symbol.for(symbolName) as CommentSymbol][0].value)
```
In this example, casting to `Symbol.for(symbolName)` to `CommentSymbol` is mandatory.
Otherwise, TypeScript won't detect that you're trying to query comments.
### Parse into an object without comments
```js
Expand Down

0 comments on commit 58d8224

Please sign in to comment.