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 5.4 aggressive union type narrowing breaks svelte-check #2316

Closed
AaronAtP123 opened this issue Mar 12, 2024 · 4 comments
Closed
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.

Comments

@AaronAtP123
Copy link

AaronAtP123 commented Mar 12, 2024

Describe the bug

TypeScript's new type narrowing behavior is causing svelte-check to emit This comparison appears to be unintentional because the types '...' and '...' have no overlap. on a variable when its value is mutated via binding.

Note this change is only currently affecting top-level variables. Union types also include boolean and enum types.

Reproduction

<script lang="ts">let checked = true;</script>
<input type="checkbox" bind:checked />
{#if checked === false}false{/if}

Expected behaviour

svelte-check should pass with no errors. Since bound expressions are type checked as covariant to the prop's type, the target variable's effective type ought to be widened to the entire union type.

System Info

svelte-check 3.6.7

  • OS: Windows 10
  • IDE: VSCode

Which package is the issue about?

svelte-check

Additional Information, eg. Screenshots

No response

@enyo
Copy link

enyo commented Mar 18, 2024

Just ran into this. This fails for me:

<script lang="ts">
  let period: 'monthly' | 'yearly' = 'yearly'
</script>

{#if period === 'monthly'}
  paid every month
{/if}

With this error:

Error: This comparison appears to be unintentional because the types '"yearly"' and '"monthly"' have no overlap. (ts)

EDIT: To temporarily work around this issue, I changed the typescript dependency to: 5.3.x

@4750moorem
Copy link

Just ran into this. This fails for me:

<script lang="ts">
  let period: 'monthly' | 'yearly' = 'yearly'
</script>

{#if period === 'monthly'}
  paid every month
{/if}

With this error:

Error: This comparison appears to be unintentional because the types '"yearly"' and '"monthly"' have no overlap. (ts)

EDIT: To temporarily work around this issue, I changed the typescript dependency to: 5.3.x

You can cast the string 'yearly' as 'monthly' | 'yearly' to get around it for now if you wanna stay on 5.4.x.

 let period: 'monthly' | 'yearly' = 'yearly' as 'monthly' | 'yearly' 

@jasonlyu123
Copy link
Member

This one is expected because there isn't any reassignment. If your actual case has an assignment somewhere that isn't binding. Can you share it here?

@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Mar 19, 2024
@danieldiekmeier
Copy link
Contributor

@dummdidumm Sadly, this is not completely fixed in the latest svelte-check v3.6.8. Maybe it's missing a special case for bind:group? At least that's the problem I'm running into. I built a small reproduction: https://stackblitz.com/edit/vitejs-vite-r64k1v?file=src%2FApp.svelte

If you run pnpm i && pnpm check, you can still see the error.

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

6 participants