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

Typescript cannot see prop types with tsconfig JSX setting as "react" #1256

Closed
petemill opened this issue Dec 1, 2021 · 4 comments
Closed
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.

Comments

@petemill
Copy link

petemill commented Dec 1, 2021

Describe the bug
Typescript is unable to see the Type for Props when tsconfig.json has compilerOptions.jsx set to "react". The use case is a single webpack bundle which has mixed react and svelte. It doesn't seem possible (at least conveniently so) to have two separate tsconfig.json projects, whilst also being able to import from one to another and resolve the issue both for IDE and the webpack build.

To Reproduce
Minimal reproduction repo at https://github.com/petemill/reproduce-svelte-types-react-jsx

<!-- src/App.svelte -->
<script lang="ts">
	export let name: string;
</script>

<main>
	<h1>Hello {name}!</h1>
</main>
// src/main.ts
import SvelteApp from './App.svelte'

const app = new SvelteApp({
	target: document.body,
	props: {
		name: 4
	}
})

tsconfig.json
note jsx property

{
		"compilerOptions": {
			"moduleResolution": "node",
			"target": "es2017",
			"importsNotUsedAsValues": "error",
			"isolatedModules": true,
			"sourceMap": true,
			"types": ["svelte"],
			"jsx": "react",
			"strict": false,
			"esModuleInterop": true,
			"skipLibCheck": true,
			"forceConsistentCasingInFileNames": true
		},	
	"include": ["src/**/*", "src/node_modules/**/*"],
	"exclude": ["node_modules/*", "__sapper__/*", "static/*"]
	}

For example a code snippet that is treated in a way you don't expect.

Expected behavior
Typescript should error with Type 'number' is not assignable to type 'string'. This is the case if compilerOptions.jsx is changed to "preserve".

image

System (please complete the following information):

  • OS: macOS
  • IDE: VSCode
  • Plugin/Package: "Svelte for VSCode"
@petemill petemill added the bug Something isn't working label Dec 1, 2021
@dummdidumm
Copy link
Member

The reason is that we internally use JSX to represent a Svelte file's types to Typescript. There's currently a rewrite ongoing to get rid of this and other limitations. No ETA on that.

@petemill
Copy link
Author

petemill commented Dec 3, 2021

The reason is that we internally use JSX to represent a Svelte file's types to Typescript. There's currently a rewrite ongoing to get rid of this and other limitations. No ETA on that.

@dummdidumm thanks for the info. Is there an issue for that, or any other way to track progress and help out?

@dummdidumm
Copy link
Member

This is the PR that contains the rewrite: #1237 . Not much to do for now other than wait on this until it lands and then help out beta-test it.

dummdidumm added a commit that referenced this issue Jan 27, 2022
Goal: Get rid of a tsx-style transformation and simplify transformations along the way
#1077
#1256
#1149

Advantages:

-   no messing with projects who use JSX for some other reason and then have conflicting JSX definitions
-   TS control flow easier to keep flowing
-   overall easier transformations of Svelte specific syntax like await, each etc.
-   better type inference in some cases, for example around svelte:component

This includes:

-   rewriting the html2jsx part of svelte2tsx
-   adjusting the language server to the new transformation
-   adding a toggle to language-server and vs code extension to turn on the new transformation. Default "off" for now, with the plan to switch it to "on" and then removing the old transformation altogether
-   ensuring tests run with both new and old transformation
-   adjusting the TypeScript plugin. It uses the new transformation already now since it's not easily possible to add an option (on/off). Should be low-impact since TS doesn't need to know much about the contents of a Svelte file, only its public API

Look at the PR of this commit to see more explanations with some of the entry points of the new transformation.
@dummdidumm
Copy link
Member

The most recent versions of the extension and svelte-check contain a new transformation behind a flag which should fix this issue. You can try out the new transformation using

  • "Svelte for VS Code" version 105.11.1 and setting svelte.plugin.svelte.useNewTransformation to true
  • svelte-check version 2.4.0 with the --use-new-transformation flag.

Please try it out, more info in #1352

@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Dec 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.
Projects
None yet
Development

No branches or pull requests

2 participants