question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type doesn't work when using array of tests

See original GitHub issue

Initial checklist

Affected packages and versions

unist-util-visit@4.1.0

Link to runnable example

https://codesandbox.io/s/react-markdown-debug-forked-sqvof

Steps to reproduce

  1. Open the codesandbox link
  2. Inspect the TypeScript error at line 25

Expected behavior

The visit method should be able to narrow the type of directiveVisitor and not throw an error.

Actual behavior

The visit method’s type is not able narrow the type of directiveVisitor because it can’t read the test ['textDirective', 'leafDirective', 'containerDirective']

Runtime

Node v14

Package manager

yarn v1

OS

macOS

Build and bundle tools

Next.js

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
wooormcommented, Nov 12, 2021

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

1reaction
IGassmanncommented, Nov 12, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test for array of string type in TypeScript - Stack Overflow
There is no any run-time type information in TypeScript (and there won't be, ... There is an issue with an empty array being...
Read more >
Test if object, array or string. · Issue #3457 · facebook/jest
Is there a way to check if a component is an object, array or string? This would be similar to chai's 'should.be.a' Ex.:...
Read more >
Better Array check with Array.isArray | SamanthaMing.com
Better Array check with Array.isArray. In JavaScript, arrays are not true arrays. They are actually objects. So you can't simply do a typeof...
Read more >
10 Most Common Mistakes That PHP Developers Make - Toptal
Are you guilty of one of these common PHP mistakes? Refer to this list next time you're debugging PHP code.
Read more >
Variables of the Table Type | TestComplete Documentation
You can use table variables to work with these arrays in tests. These arrays do not correspond to arrays in JavaScript, JScript and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found