-
-
Notifications
You must be signed in to change notification settings - Fork 223
(feat) infer slot/events type from props #952
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) infer slot/events type from props #952
Conversation
This changes the generation for accessing the events/slots of a component. Instead of using `instanceOf`, the component instance is created using the constructor and passing the props. This enables TypeScript to infer generic relationships between props and slots/events. sveltejs#945 Note that this change does not cover all cases: If someone forwards a slot or an event whose type is generic and needs to be infered, that type will remain unknown, because reinstantiating the context in the `return { props: .. }` step is really hard.
|
The only solution I can think of now is to declare a new variable. Something similar to if-scope or pass it as a parameter to an immediately invoked function. const __propsArgs = { a }
() =>{
const a = new Component({ props: __propsArgs }).$$slotdef['default']
}or ((__propsArgs) =>{
const a = new Component({ props: __propsArgs }).$$slotdef['default']
})({ a }) |
|
Yeah sounds like the only option. So there needs to be some logic to track which props there are both on the default and the names slots, and if there's one that's duplicated, construct a immediately invoked function from the default slot and reuse that in all the inner slots. |
|
Ok I reused the ifScope logic here, worked out well, although I still feel that this code is getting more and more complex - if it's just inevitable or I'm writing shitty code, can't tell for sure right now 😄 @jasonlyu123 could you have a look? |
jasonlyu123
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly working great. Found a small problem when an attribute is empty. And some minor issues to discuss.
|
These were great suggestions, thanks! Especially that string template comment was a moment of enlightment 😄 |
|
I noticed that a string value is inferred as "any" type in the slot prop! |
|
Should be fixed with the latest release |
|
I am using the latest release, all values get inferred normally, but when using a string value it inferred as "any" type not "string" type |
|
Can't reproduce. What's your version? Latest is 104.10.1 . If the issue persists with the latest version after restarting VS Code, please open a separate issue with detailed instructions. |
|
Great, I tried that one more time and it's working fine now. |
This changes the generation for accessing the events/slots of a component. Instead of using
instanceOf, the component instance is created using the constructor and passing the props. This enables TypeScript to infer generic relationships between props and slots/events.#945
Note that this change does not cover all cases: If someone forwards a slot or an event whose type is generic and needs to be infered, that type will remain unknown, because reinstantiating the context in the
return { props: .. }step is really hard.TODO:
How to fix the "varialbe used before instantiation"-error that occurs when we tranform
<Foo prop={prop} let:prop>? The transformation forlet:propisconst prop = new Foo({.. props: {prop: prop}})which TS doesn't like. @jasonlyu123 any ideas?