Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My IDE report Error Its type '"p"' is not a valid JSX element type. #3984

Closed
XLearner opened this issue Apr 25, 2023 · 18 comments
Closed

My IDE report Error Its type '"p"' is not a valid JSX element type. #3984

XLearner opened this issue Apr 25, 2023 · 18 comments
Labels

Comments

@XLearner
Copy link

I imported preact into my project and the project worked successfully. However, the VSCode had always the following error message:

image

I can use // @ts-expect-error to shield this error, but I want to get a more gentle way to deal with this.

Could anyone help? 🤜🏽

@rschristian
Copy link
Member

Have you restarted the TS language server?

What does your tsconfig.json look like?

@XLearner
Copy link
Author

Here is my tsconfig.json:

{
  "compilerOptions": {
    "sourceMap": true,
    "jsx": "preserve",
    "jsxImportSource": "preact",
    "jsxFactory": "h",
    "jsxFragmentFactory": "Fragment",
    "target": "es5",
    "module": "commonjs",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

@rschristian
Copy link
Member

Remove "jsxImportSource": "preact", it shouldn't be set unless you're using the automatic JSX transform. I don't think that'll be a problem per se though.

I have to imagine it's just a temporary bug in your editor, as everything else it looks fine. Give your editor a restart it you haven't already, or try to reproduce the issue in something like codesandbox if it persists.

@XLearner
Copy link
Author

It is no work of removing "jsxImportSource": "preact". And I've tried to restart TS Server, during the restarting process, this error message is gone, but appears after the restarting is done.
I try to reproduce the issue in a codesandbox but fail, I notice the declaration tips are different, like the following image. The reason for the difference is that I import h by hand, if I don't do that there will have a problem in runtime that can not transform React.createElement to h. However, even if I remove the h, this issue is still there.😭

image

@rschristian
Copy link
Member

You can try adding "paths" to your tsconfig.json maybe, as we show here: https://preactjs.com/guide/v10/typescript#typescript-preactcompat-configuration

Otherwise not sure; not a VSCode user myself and it seems like it might be some misconfiguration in your editor specifically.

@XLearner
Copy link
Author

Thank you very much for your patient reply. I have tried all the methods mentioned in the document, but none of them have worked. I will use comments // @ts-expect-error as a temporary solution to avoid this error, and then I will look for a solution later. Thank you.

@marvinhagemeister
Copy link
Member

Can you share a small repo or codesandbox where the issue can be reproduced? That will make it much easier to narrow it down.

@lm-maxfab
Copy link

I have the same problem of VSCode saying "'div' cannot be used as a JSX component.". Here is a repo from which I reproduce the error message, it is a minimal preact x typescript configured following preact docs : https://github.com/lm-maxfab/preact-test

No problem at build time though, the compilation via esbuild goes well and the built output works.

image

image

@davidmz
Copy link

davidmz commented Apr 25, 2023

Here is my minimal repository with the same problem: https://github.com/davidmz/preact-types-test

image

@rschristian
Copy link
Member

I'm a bit surprised to see 3 (seemingly unrelated) people have the same issue. Is this specifically an issue with the latest or recent versions of Preact? Do you still see this if you go back a bit (say, mid v10)?

@marvinhagemeister
Copy link
Member

@lm-maxfab Thanks for sharing that repo. Weirdly I'm unable to reproduce that on my end. VSCode finds the correct JSX types. Works in both the stable release line and vscode insiders for me. Note that esbuild doesn't do any type checking. It just deletes the types, so any type errors would be ignored by it.

Screenshot 2023-04-26 at 07 22 20

@davidmz I'm getting a 404 on that link. Is the repo private?

@davidmz
Copy link

davidmz commented Apr 26, 2023

@marvinhagemeister oh, sorry, it really was private. I've now made it public.

But now I see the same error with clean pnpm create vite test-app --template preact-ts, so I doubt that Preact is the problem. It looks more like something is broken in our VSCode settings...

@davidmz
Copy link

davidmz commented Apr 26, 2023

Well, my VSCode had the ms-vscode.vscode-typescript-next extension installed. I disabled it, and that seemed to fix the problem, at least for me. Sorry, I didn't check that at first.

@rschristian
Copy link
Member

Interesting, maybe should raise this on that extension's tracker.

@lm-maxfab and @XLearner, do you have this installed too by chance?

@davidmz
Copy link

davidmz commented Apr 26, 2023

I see there is a recent bug in Typescript that looks similar: microsoft/TypeScript#53939. Maybe the extension was using the nightly version with this bug.

@lm-maxfab
Copy link

lm-maxfab commented Apr 26, 2023

@lm-maxfab and @XLearner, do you have this installed too by chance?

Yep, had it installed too, everything's fine when disabling it. Thanks a lot!

@XLearner
Copy link
Author

Interesting, maybe should raise this on that extension's tracker.

@lm-maxfab and @XLearner, do you have this installed too by chance?

Wow, that's really the key to the problem. After reloading this plugin, there is no issue anymore. Thank you very much!

@JoviDeCroock
Copy link
Member

Closing this as it seems to be a genuine TS related bug, for anyone finding this thread, disabling [ms-vscode.vscode-typescript-next](https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-typescript-next) fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants