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

svelte-check/typescript detecting type mismatch where there shouldn't be one #8860

Closed
MathiasFrost opened this issue Jun 27, 2023 · 13 comments · Fixed by #8872
Closed

svelte-check/typescript detecting type mismatch where there shouldn't be one #8860

MathiasFrost opened this issue Jun 27, 2023 · 13 comments · Fixed by #8872

Comments

@MathiasFrost
Copy link

Describe the bug

svelte-check/typescript detecting mismatch where there shouldn't be one

Reproduction

<script lang="ts">
	import { createEventDispatcher } from "svelte";

	/** */
	type T = $$Generic;

	/** */
	const dispatch = createEventDispatcher<{ change: T }>();

	export let value: T;

	/** */
	function test(): void {
		dispatch("change", value);
	}
</script>

Logs

Error: Argument of type '["resolved", T]' is not assignable to parameter of type '[T] extends [never] ? [type: "resolved", parameter?: null | undefined, options?: DispatchOptions | undefined] : null extends T ? [type: "resolved", parameter?: T | undefined, options?: DispatchOptions | undefined] : undefined extends T ? [type: ...] : [type: ...]'. (ts)
        data.onInvoked = (promise) => dispatch("invoked", promise);
        data.onResolved = (res) => dispatch("resolved", res);
        data.onReject = (e) => dispatch("rejected", e);

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 15.99 GB / 31.93 GB
  Binaries:
    Node: 18.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.18 - ~\AppData\Local\Yarn\bin\yarn.CMD
    npm: 8.4.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.5.1 - ~\AppData\Local\pnpm\pnpm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (114.0.1823.58)
  npmPackages:
    svelte: ^4.0.0 => 4.0.0

Severity

blocking an upgrade

@MathiasFrost MathiasFrost changed the title svelte-check/typescript detecting type mismatch where there shouldn svelte-check/typescript detecting type mismatch where there shouldn't be one Jun 27, 2023
@dummdidumm
Copy link
Member

Gah, this is tricky. I think the problem is that TypeScript doesn't know whether or not the generic extends never, so it will not narrow the extends clause to any of the possibilities. We probably need to find some type hack to work around this.

@MathiasFrost
Copy link
Author

Gah, this is tricky. I think the problem is that TypeScript doesn't know whether or not the generic extends never, so it will not narrow the extends clause to any of the possibilities. We probably need to find some type hack to work around this.

I am not a master of TypeScript generics; is there a way for me to put a constraint on the generic to tell TypeScript that it never extends never?

@VismaTobbe
Copy link

Type check in Rider (IntelliJ) is complaining about Argument type "save" is not assignable to parameter type any. It does pass svelte-check though. Maybe this is a separate ticket or even svelte plugin related?

  const eventDispatcher = createEventDispatcher<{ save: boolean }>();
  eventDispatcher("save", true);

@dummdidumm
Copy link
Member

Narrowed this down to microsoft/TypeScript#54806, let's see what happens (I fear this is a TS limitation)

@MathiasFrost
Copy link
Author

Type check in Rider (IntelliJ) is complaining about Argument type "save" is not assignable to parameter type any. It does pass svelte-check though. Maybe this is a separate ticket or even svelte plugin related?

  const eventDispatcher = createEventDispatcher<{ save: boolean }>();
  eventDispatcher("save", true);

That snippet should be working. JetBrain's Svelte plugin is unfortunately unusable at this moment and I would advise against using it; What you are experiencing is just one of many things that doesn't work.
There seem to be some effort now to focus on it thought, so I'm hoping it'll get better soon.

@MathiasFrost
Copy link
Author

Narrowed this down to microsoft/TypeScript#54806, let's see what happens (I fear this is a TS limitation)

Ah I understand now. Yeah, maybe better to see what TypeScript can do about it, since that does seem like something they probably would want to fix as well.

@VismaTobbe
Copy link

VismaTobbe commented Jun 28, 2023

Type check in Rider (IntelliJ) is complaining about Argument type "save" is not assignable to parameter type any. It does pass svelte-check though. Maybe this is a separate ticket or even svelte plugin related?

  const eventDispatcher = createEventDispatcher<{ save: boolean }>();
  eventDispatcher("save", true);

That snippet should be working. JetBrain's Svelte plugin is unfortunately unusable at this moment and I would advise against using it; What you are experiencing is just one of many things that doesn't work. There seem to be some effort now to focus on it thought, so I'm hoping it'll get better soon.

Interesting, been using that plugin over a year in my Svelte project (not Sveltekit) with not to much drama.

@dummdidumm
Copy link
Member

Does that same snippet work within a .ts file in Rider? If not, I fear that Jetbrains uses a different way to do TypeScript checking and doesn't support the specific way the types are written.

@VismaTobbe
Copy link

Does that same snippet work within a .ts file in Rider? If not, I fear that Jetbrains uses a different way to do TypeScript checking and doesn't support the specific way the types are written.

Yes, it works in plain .ts file, only a problem inside <script lang="ts">

@dummdidumm
Copy link
Member

Then that sounds like another case where the plugin doesn't quite work right - though I remember that they just announced to use the svelte-language-server for diagnostics, which would mean it should work. Mhm.. Either way, nothing we can influence.

dummdidumm added a commit that referenced this issue Jun 28, 2023
fixes #8860

This contains a small but unfortunately unavoidable breaking change: If you used `never` to type that the second parameter shouldn't be set (which the docs recommended for a short time), then you need to change that to `null`
@MathiasFrost
Copy link
Author

MathiasFrost commented Jun 28, 2023

Type check in Rider (IntelliJ) is complaining about Argument type "save" is not assignable to parameter type any. It does pass svelte-check though. Maybe this is a separate ticket or even svelte plugin related?

  const eventDispatcher = createEventDispatcher<{ save: boolean }>();
  eventDispatcher("save", true);

That snippet should be working. JetBrain's Svelte plugin is unfortunately unusable at this moment and I would advise against using it; What you are experiencing is just one of many things that doesn't work. There seem to be some effort now to focus on it thought, so I'm hoping it'll get better soon.

Interesting, been using that plugin over a year in my Svelte project (not Sveltekit) with not to much drama.

Been using it myself at work for about 2 years on and off (have given up and used VSCode last 6 months), and I am sure on smaller projects you have a decent chance of not experiencing much issues, but dive into a larger project and you'll hit wall after wall:

  • Like this
  • And this
  • And this
  • And this
  • Don't remember if there is an issue for it, but $$Generic or generics="T" is not recognized at all.
  • The plugin hasn't been updated much since before SvelteKit 1.0, so open up a standard SvelteKit 1.0 project in JetBrains IDE and you'll see a lot of warnings and errors.
  • Working with npm packages packaged with SvelteKit won't work with intellisense at all

If they can manage to use the svelte language server instead of their own it would help a lot, since without it they basically have to make their own from scratch. I'm not sure how they would do it, since JetBrain's language tools uses their own little programming language, but last WebStorm update they said Vue now uses Volar so maybe they figured it out (haven't tried Vue in a while, let alone with WebStorm).
Me and my collogue have opened a bunch of issues on YouTrack for these and have seen little to no activity on them last 2 years, but starting to see more now, so fingers crossed :)

@MathiasFrost
Copy link
Author

@dummdidumm Do you mind me asking why in the playground for the TypeScript issue (and in svelte source code for that matter), you use [Type] extends [never] and not Type extends never. I am very curious.

@dummdidumm
Copy link
Member

This blog post explains it quite nicely: https://www.zhenghao.io/posts/ts-never#how-to-check-for-never

dummdidumm added a commit that referenced this issue Jun 28, 2023
…function types (#8872)

fixes #8860

This contains a small but unfortunately unavoidable breaking change: If you used `never` to type that the second parameter of `createEventDispatcher` shouldn't be set or that the action accepts no parameters (which the docs recommended for a short time), then you need to change that to `null` and `undefined` respectively
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants