Giter VIP home page Giter VIP logo

Comments (7)

IGassmann avatar IGassmann commented on May 26, 2024 1

@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.

wooorm avatar wooorm commented on May 26, 2024 1

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.

wooorm avatar wooorm commented on May 26, 2024

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.

IGassmann avatar IGassmann commented on May 26, 2024

@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.

wooorm avatar wooorm commented on May 26, 2024

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.

wooorm avatar wooorm commented on May 26, 2024

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.

wooorm avatar wooorm commented on May 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.