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

Support @ts-ignore comments in template markup #1026

Open
madeleineostoja opened this issue May 24, 2021 · 11 comments
Open

Support @ts-ignore comments in template markup #1026

madeleineostoja opened this issue May 24, 2021 · 11 comments
Labels
feature request New feature or request

Comments

@madeleineostoja
Copy link

Is your feature request related to a problem? Please describe.
Since svelte can't (yet?) support typescript expressions in component template markup, it's impossible to provide typings/coercions/etc in template logic blocks. This results in seas of red warnings and errors in the editor and svelte-check.

Describe the solution you'd like
Assuming giving Svelte full typescript support in markup is either not going to happen or not happening any time soon, it would at least be nice to support @ts-ignore HTML comments to skip typescript warnings for the following line of code.

Describe alternatives you've considered
The only real alternative is to disable typescript diagnostics in language-tools entirely, which is obviously less than ideal

@dummdidumm
Copy link
Member

dummdidumm commented May 25, 2021

This is tricky if not impossible for some situations, for example if you have your attributes spread out over multiple lines. @ts-ignore only works for the next line, and html comments within the < > of a tag are invalid. What is the sea of errors you talk about specifically? Can this be solved by more explicit typing up front?

@madeleineostoja
Copy link
Author

Hmm that’s fair, I hadn’t thought of attributes spreading over multiple lines. That would make @ts-ignore comments fragile and unintuitive.

Happy to just close this and hope for type support in template markup instead

@lovasoa
Copy link

lovasoa commented Dec 30, 2021

I use this nasty trick to disable type checking of a single attribue :

<script lang="ts">
	const notypecheck = (x:any)=>x;
</script>

<MyComponent {...notypecheck({
    attribute:  value
}) />

@rogadev
Copy link

rogadev commented Feb 27, 2023

I use this nasty trick to disable type checking of a single attribute :

<script lang="ts">
	const notypecheck = (x:any)=>x;
</script>

<MyComponent {...notypecheck({
    attribute:  value
}) />

How would you do that for something like this:

{#each columns as blok}
  <div class="flex-auto px-6">
    <StoryblokComponent {blok} /> <!-- (property) StoryblokComponentProps.blok: SbBlokData
      Type 'string' is not assignable to type 'SbBlokData'.  -->
  </div>
{/each}

And if a method of doing so exists, is it easier than:

{#each columns as blok}
  <div class="flex-auto px-6">
    <!-- @ts-ignore -->
    <StoryblokComponent {blok} />
  </div>
{/each}

@dummdidumm dummdidumm reopened this Feb 28, 2023
@dummdidumm dummdidumm added the feature request New feature or request label Feb 28, 2023
@dummdidumm
Copy link
Member

If this would be implemented, it would be through a <!-- @ts-ignore --> comment. Since comments cannot be placed within the tag itself, the question is what the more intuitive/wanted behavior here is:

<!-- @ts-ignore -->
<ComponentThatHasError
  {propThatHasError}
/>

should the error from propThatHasError also be ignored, or only the error from <ComponentThatHasError? My feeling is that everything inside the component should be ignored, because

  • no other way to silence through a comment
  • no depending on how things are formatted, this is inconsistent: if propThatHasError is on the same line, that error is silenced, too, else it isn't

Thoughts welcome

@madeleineostoja
Copy link
Author

That would certainly be the intuitive behavior, but it also feels fragile? I don’t know implementation details though.

I guess the wider question is has there been any momentum on allowing ts in template expressions? That would make this issue kind of moot

@dummdidumm
Copy link
Member

Good point, with TS inside template expressions you could do casting instead, which would probably solve this for 90% of use cases. What's left would be things like silencing errors like "don't know this component" or "this component/element doesn't have prop X", which are probably rare.

@crushingCodes
Copy link

I couldn't get the suggestion above to work, but luckily the component was my own, so i was able to get around it using the following:

MyComponent.svelte

<script lang="ts">
	import type { IThing } from '$lib/server/types';

	export let value: unknown;

	const thing: IThing = value as IThing;
</script>

Consumer.svelte

<MyComponent value={value} />

@LorenzoBloedow
Copy link

I have a suggestion: we could prefix attributes with ts-ignore- or just use the directive ts-ignore-all to ignore the entire component.

@nikonikoniko
Copy link

nikonikoniko commented Sep 16, 2024

I have managed to sneak in a @ts-ignore in like this:

<Input
  type="file"
  multiple
  {...attrs}
  {...{
    ...{} 
    /* @ts-ignore */
  }}
  bind:files={$formData.file}
/>

I used this to replace the useful trick from @lovasoa

@chaychoong
Copy link

I tried this, seems to work

{/* @ts-ignore */ null}
<label htmlFor="example">Example</label>

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

No branches or pull requests

8 participants