You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm here to talk about my experiences with using Vue and Typescript.
The version of Vue that I'm using is 3.4.21 and Vue - Official. I'm here to talk about some things that aren't great when it comes to using typescript with vue. I hope these problems can be solved.
My TS experience is slowed down my the mistakes mentioned here.
Directives aren't typed well
I have noticed that you aren't able to type modifiers or args in directives. I don't know why, but this has dismissed the usefulness of directives. Not only that when it comes to the global ones there is no way to assure that people can't type in bad modifiers in global directives. I believe this is a problem that needs to be solved. Args and modifiers are the most important part of a directive. They should be typeable. Otherwise, TS won't be able to tell about bugs.
Tag refs aren't associated with proper Element types
I have noticed that when I try to type in a function inside of refs, the first parameter is always. Element.
<dialog :ref="(el:Element)=>el.target">
</dialog>
This means I always have to write some code to check what the element is to be able to use it or do type casting for each ref.
In other words I have to write this.
The code I have written right now is simpler than the one before.
The problem is a fixable one. A function Ref has to be typed as on overload instead of what we have now, which is this. I also think it should be separated into its own type.
This function ref should also work inside the template.
Event objects aren't typed based on Event type
One thing I've noticed from React that I don't think other frameworks have tried is typing Event Objects based on the types of handlers and tags. In Vue, every event is typed as simply Event.
In React, there is a type for every event. I can easily access the event target without ever having to check for the instance of the event target.
In this example the BaseSyntheticEvent type is the type for all other events. The SyntheticEvent inherits from that type, making it easy to type all other events in React. In React there are many kinds of events. One for form FormEvent one for mouse MouseEvent and more.
Each of those types is eventually used to type the EventHandler type.
typeEventHandler<EextendsEvent>=(e:E)=>void
The type above is used to create many kinds of typed event handlers.
Doing this for makes it easy for developers to not only understand the elements that are written, but it makes the code it templates type-safe.
Writing an element grabber type.
There is a type that uses the name of a tag to grab element attribute types from a tag using an object that maps each tag name to its associated attributes. This type is useful for getting not just the attributes for one type but the attributes for multiple types. It created like this in Astro.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm here to talk about my experiences with using Vue and Typescript.
The version of Vue that I'm using is 3.4.21 and Vue - Official. I'm here to talk about some things that aren't great when it comes to using typescript with vue. I hope these problems can be solved.
My TS experience is slowed down my the mistakes mentioned here.
Directives aren't typed well
I have noticed that you aren't able to type modifiers or args in directives. I don't know why, but this has dismissed the usefulness of directives. Not only that when it comes to the global ones there is no way to assure that people can't type in bad modifiers in global directives. I believe this is a problem that needs to be solved. Args and modifiers are the most important part of a directive. They should be typeable. Otherwise, TS won't be able to tell about bugs.
Tag refs aren't associated with proper Element types
I have noticed that when I try to type in a function inside of refs, the first parameter is always.
Element
.This means I always have to write some code to check what the element is to be able to use it or do type casting for each ref.
In other words I have to write this.
Or this.
This is tedious.
If the dialog element that is passed was typed properly. It would look something like this.
This means all I have to do is write this code. When I decide to write a method for the
dialog
element.The code I have written right now is simpler than the one before.
The problem is a fixable one. A function Ref has to be typed as on overload instead of what we have now, which is this. I also think it should be separated into its own type.
Instead of this
We can have this
This function ref should also work inside the template.
Event objects aren't typed based on Event type
One thing I've noticed from React that I don't think other frameworks have tried is typing Event Objects based on the types of handlers and tags. In Vue, every event is typed as simply
Event
.In React, there is a type for every event. I can easily access the event target without ever having to check for the instance of the event target.
The reason this is possible is that the function is inferred to be.
These kinds of types are written based on each tag.
The benefit of doing types like this is that developers can get introduced to each Element's API in a faster and more satisfying way.
The type that react uses is something like this.
In this example the
BaseSyntheticEvent
type is the type for all other events. TheSyntheticEvent
inherits from that type, making it easy to type all other events in React. In React there are many kinds of events. One for formFormEvent
one for mouseMouseEvent
and more.Each of those types is eventually used to type the
EventHandler
type.The type above is used to create many kinds of typed event handlers.
Doing this for makes it easy for developers to not only understand the elements that are written, but it makes the code it templates type-safe.
Writing an element grabber type.
There is a type that uses the name of a tag to grab element attribute types from a tag using an object that maps each tag name to its associated attributes. This type is useful for getting not just the attributes for one type but the attributes for multiple types. It created like this in
Astro
.The example above is a simplified version. But the one that comes from Vue must not allow attributes that are specific to Vue. As in no
ref
.Beta Was this translation helpful? Give feedback.
All reactions