Allow implicit React import?
See original GitHub issueSince 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.
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:
- Created 2 years ago
- Comments:9 (3 by maintainers)
Top 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 >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
Sure, at a guess we need two things, a way to disable the magic and some magic.
First disabling the magic: extend
TwoslashShikiOptions
inshiki-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 inrunTwoSlash
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’:
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.
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.