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 Generics #2204

Closed
jycouet opened this issue Nov 16, 2023 · 5 comments
Closed

Typescript Generics #2204

jycouet opened this issue Nov 16, 2023 · 5 comments
Labels
bug Something isn't working Fixed Fixed in master branch. Pending production release.

Comments

@jycouet
Copy link

jycouet commented Nov 16, 2023

Describe the bug

In https://github.com/dummdidumm/rfcs/blob/ts-typedefs-within-svelte-components/text/ts-typing-props-slots-events.md#generics I was able to learn about generics.

I would like to have:

<script lang="ts" generics="T extends Record<string, any>">

Unfortunalty I get ERROR: Unterminated string literal. (probably due to < and > parsing).


Timing seems great, I'm wondering if sveltejs/svelte#9482 will not solve this at the same time?

Reproduction

https://github.com/jycouet/kit-generics

Logs

No response

System Info

System:
    OS: macOS 14.1.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 17.00 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.2.0 - ~/.nvm/versions/node/v21.2.0/bin/node
    npm: 10.2.3 - ~/.nvm/versions/node/v21.2.0/bin/npm
    pnpm: 8.10.5 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 119.0.6045.123
    Safari: 17.1
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.1.1 
    @sveltejs/kit: ^1.27.4 => 1.27.6 
    svelte: ^4.0.5 => 4.2.3 
    vite: ^4.4.2 => 4.5.0

Severity

annoyance

Additional Information

No response

@david-plugge
Copy link
Contributor

david-plugge commented Nov 16, 2023

You can use $$Generic:

<script lang="ts">
	type T = $$Generic<Record<string, number>>;

	export let data: T;

	export let keys: (keyof T)[];
</script>
<script lang="ts">
	import Test from './test.svelte';
</script>

<Test data={{ a: 1 }} keys={['a']}></Test>

@brunnerh
Copy link
Member

That is deprecated syntax.

Discarded alternative 1
You use new reserved type called $$Generic.

@jycouet
Copy link
Author

jycouet commented Nov 16, 2023

Thx @david-plugge I confirm that it's working well 👍
As @brunnerh highlighted, it's deprecated, maybe the new PR will tackle this?

In the mean time, I'm good with $$generic thx a lot

@dummdidumm dummdidumm transferred this issue from sveltejs/kit Nov 16, 2023
@dummdidumm dummdidumm added the bug Something isn't working label Nov 16, 2023
@dummdidumm
Copy link
Member

Fixed on the language tools side, release pending - but this likely also needs changes in svelte-preprocess / svelte / any other tool that uses a regex to extract the contents of a script or style tag. The regex needs to be adjusted to handle the "> inside an attribute string" case

dummdidumm added a commit that referenced this issue Nov 16, 2023
take into account boolean attributes
#2204
dummdidumm added a commit to sveltejs/svelte that referenced this issue Nov 16, 2023
dummdidumm added a commit to sveltejs/vite-plugin-svelte that referenced this issue Nov 16, 2023
...when detecting script language
Related to sveltejs/language-tools#2204
Rich-Harris pushed a commit to sveltejs/svelte that referenced this issue Nov 16, 2023
dummdidumm added a commit to sveltejs/svelte that referenced this issue Nov 16, 2023
dummdidumm added a commit to sveltejs/svelte that referenced this issue Nov 17, 2023
Related to sveltejs/language-tools#2204 / sveltejs/language-tools#2039
The Svelte 5 version of #9486 and #9498

---------

Co-authored-by: Rich Harris <[email protected]>
Co-authored-by: Rich Harris <[email protected]>
@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Nov 21, 2023
dominikg added a commit to sveltejs/vite-plugin-svelte that referenced this issue Nov 22, 2023
* fix: handle closing tags in attributes

...when detecting script language
Related to sveltejs/language-tools#2204

* Update packages/vite-plugin-svelte/src/utils/compile.js

Co-authored-by: Dominik G. <[email protected]>

---------

Co-authored-by: Dominik G. <[email protected]>
dummdidumm added a commit to sveltejs/prettier-plugin-svelte that referenced this issue Nov 22, 2023
@dummdidumm
Copy link
Member

This works now using the latest version of the extension/svelte-check/svelte

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

4 participants