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

feat: add a default types in the $$Generic #2184

Closed
wants to merge 1 commit into from

Conversation

fedorovvvv
Copy link

Hi!✨
A common problem with $$Generic is that there was no way to set a default type

I tried to add more functionality and now it is possible to set the default type as the second argument to $$Generic

type T = $$Generic<number, 2>
interface $$Props {
    value?:T
    result?: T extends 2 ? 'AAAAAA' : 'OOOOO'
}
export let value:$$Props['value'] = 2

I think this will take types in svelte one step further and open up a lot of new possibilities
I can't live without default types))🥲

@fedorovvvv
Copy link
Author

I fixed the condition, error output and ran all the tests. Everything's fine.

I hope you will accept this PR or we will improve it together💛

@fedorovvvv
Copy link
Author

No one has used this syntax yet because of the error condition, so the chance of affecting projects is minimal if not 0

@dummdidumm
Copy link
Member

We're currently leaning towards going with the generics attribute solution instead, which means $$Generics would be deprecated. Given that I'm hesitant to merge this - with the generics attribute, this is straightforward to express:

<script generics="T extends number = 2">
  interface $$Props {
    value?:T
    result?: T extends 2 ? 'AAAAAA' : 'OOOOO'
  }
  export let value:$$Props['value'] = 2
</script>

Or using runes:

<script generics="T extends number = 2">
  let { value = 2, result } = $props<{
    value?:T
    result?: T extends 2 ? 'AAAAAA' : 'OOOOO'
  }>();
</script>

@fedorovvvv
Copy link
Author

Whoa!
I wasted an evening digging up $$$Generic😂

Is generics written about in the documentation?

Thanks💜

@fedorovvvv fedorovvvv closed this Oct 17, 2023
@dummdidumm
Copy link
Member

No official documentation yet, but it's described in the RFC

@fedorovvvv
Copy link
Author

Wow, these are absolute new solutions for types, thank you)

I'm surprised I haven't seen this before

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

Successfully merging this pull request may close these issues.

2 participants