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.

Allow implicit React import?

See original GitHub issue

Since JSX-Transform, I have not used to importing React explicitly in my codebases. I want to treat the same with my code blocks i.e. I want to configure so that React is imported implicitly in my tsx code blocks.

errrrrr

Currently, I think best I can do is to cut imports like,

import React from 'react'
// ---cut---

But I don’t think it’s the best option. Or am I missing something?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ortacommented, Jun 23, 2021

Sure, at a guess we need two things, a way to disable the magic and some magic.

First disabling the magic: extend TwoslashShikiOptions in shiki-twoslash to have a new property, I don’t have time to think the name through right now, but lets just call it ‘dontAddImplicitGlobals’. Any of this work needs to be able to be turned off.

Second:

  • check if // @allowUmdGlobalAccess works fine in a normal jsx/tsx file with React code - if it does then we really just need to jam that in on line one at shiki-twoslash level before passing that code into shiki - so somewhere in runTwoSlash before we call twoslash. It’ll get removed automatically and we don’t have to think about it at different levels.

  • If it does not work. I think we need a way to pass code in and then ignore it on the way out. That could be something like adding support for an inline version of ‘cut’:

    import React from "react" // --- cut
    

    Where twoslash would see it on the way in, but not pass it back out - this would need to happen over in the TS website. Then shiki-twoslash’s job would be to add a prefix of that import in react files with the cut comment line specifically removed.

0reactions
frencojobscommented, Jun 24, 2021

Hi @orta, I implemented a less busier solution by combining all the cuts before passing the code to twoslasher. Because otherwise, to implement inline cut comments requires the need to shift the metadata positions after cutting all theese lines, which I think is overkill just for this feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introducing the New JSX Transform – React Blog
With the new transform, you can use JSX without importing React. · Depending on your setup, its compiled output may slightly improve the...
Read more >
You no longer need to import React from 'react'
This allows us to not import React, however, you'll need to import React to use Hooks and other exports that React provides.
Read more >
TypeScript 2.1 Feature of Implicit Import Not Working
When I install types for my module (React in this case) it works just fine. Here's my package.json: { "name": "my_app_name", "version": " ......
Read more >
Why Did And Don't You Need To Import React - Chak Shun Yu
Since React v17.0, you don't have to import React anymore in your JSX files. But why was this seemingly unused import when creating...
Read more >
TSConfig Option: allowSyntheticDefaultImports - TypeScript
TSConfig. allowSyntheticDefaultImports. When set to true, allowSyntheticDefaultImports allows you to write an import like: ts. import React from "react";.
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