Type doesn't work when using array of tests
See original GitHub issueInitial checklist
- I read the support docs
- I read the contributing guide
- I agree to follow the code of conduct
- I searched issues and couldn’t find anything (or linked relevant results below)
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
- Open the codesandbox link
- 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:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem is that TS sees an array (
['a', 'b']
) asstring[]
, and then we can’t do anything with it anymore. We could try and add support foras const
syntax:(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
@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.