Comments (7)
@wooorm The typo mislead me. I thought search
was equal to ['textDirective', 'leafDirective', 'containerDirective']
and the visitor function was used as the third parameter.
Your solution does work. Thanks! However, ideally it would be great that the type would be able to properly narrow the visitor function. Although, I don't know if that's actually possible with TypeScript.
from unist-util-visit.
The problem is that TS sees an array (['a', 'b']
) as string[]
, and then we can’t do anything with it anymore. We could try and add support for as const
syntax:
const search: Test = [
"textDirective",
"leafDirective",
"containerDirective"
] as const
visit(tree, search, (node) => {
(doesn’t work), but then again I don’t think this behavior is intuitive, I don’t think folks would think that they’d have to do that
from unist-util-visit.
TS doesn’t like arrays of things.
You can always get around it:
visit(tree, (node) => {
if (node.type === "textDirective" || node.type === "leafDirective" || node.type === "containerDirective") {
// things.
}
})
from unist-util-visit.
@wooorm this still fails for what I'm trying to achieve since the node
parameter isn't properly typed to include the properties name
and attributes
.
from unist-util-visit.
I don’t know what you’re trying to achieve? It’s apparently different from what you explained before or have in the codesandbox?
The node
parameter is properly typed if you properly type tree
P.S.: I had a typo in the above code where search
existed but shouldn’t have existed
from unist-util-visit.
Here is your CS changed to make it work: https://codesandbox.io/s/react-markdown-debug-forked-4iimg.
For more info on using types with remark-directive
, see its readme: https://github.com/remarkjs/remark-directive#types
from unist-util-visit.
I believe this is a typescript limitation that can’t be changed. I’m open to documenting it. Or adding as const
support.
from unist-util-visit.
Related Issues (13)
- Is the API synchronous? HOT 2
- Can transformations be immutable? HOT 6
- Special character parsing HOT 2
- Add types HOT 7
- [Types]: type of parent should be Parent HOT 3
- Standalone documentation HOT 3
- Error [ERR_REQUIRE_ESM]: Must use import to load ES Module HOT 14
- NPM readme example still uses require() HOT 3
- Causes tsc to stall indefinitely HOT 16
- Cannot find module 'unist-util-is/lib/index.js' or its corresponding type declarations HOT 5
- Types not regonized on nodes HOT 4
- Note on mutation HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unist-util-visit.