Unlike using JavaScript, writing Vue programs in Typescript requires an understanding of Vue-related types, most of which are written in the @vue/runtime-core package.
Component
A Vue page is composed of a component, and the component’s class in Vue is Component
, which inherits from ComponentOptions
, FunctionalComponent
and ComponentPublicInstanceConstructor
.
Of these, ComponentOptions
inherits from ComponentOptionsBase
, which is the declarative options component we often write that contains properties such as data
, methods
, etc.
FunctionalComponent
is a functional component, and ComponentPublicInstanceConstructor
is an instance constructor.
ComponentOptions
inherits from ComponentCustomOptions
, an interface that is empty in the Vue source code, with which we can customize the properties in the Vue component options, as in the example in the source code.
The defineComponent
function that we use when defining components is used to help us with the type declaration of component options, and it accepts ComponentOptionsWithoutProps
, ComponentOptionsWithArrayProps
or ComponentOptionsWithObjectProps
as option parameters. They all inherit from ComponentOptionsBase
, but have different forms of declaring props. This function can also accept the setup function.
The defineComponent
function returns the DefineComponent
class object, which is the intersection class object of ComponentOptionsBase
and ComponentPublicInstanceConstructor
.
|
|
CreateAppFunction
In V3, the launch of a page usually starts with createApp, which has the following type declaration.
It accepts a Component
and property as parameters and returns an App
.
App
The App
instance is a Vue top-level object through which you can set shared properties, set plugins, register components, set compilation options, set error handling functions, etc.
ComponentPublicInstance
ComponentPublicInstance
is a component instance containing properties such as $el
, emit
, props
, etc. Vue uses Component
as a template and creates ComponentPublicInstance
.
Its type is defined as
|
|
where $options
is the intersection of the ComponentOptionsBase
class object (containing a renderer
method for functional components, if any) and the MergedComponentOptionsOverride (hook function) class when we write the component.
P
, ShallowUnwrapRef
, UnwrapNestedRefs
, ExtractComputedReturns
, M
help us to read the data properties of the component instance with this[…] to read the data properties and methods of the component instance.
ComponentCustomProperties
is an empty interface in the source code that allows us to customize the properties on the component instance. Example.
The $
property is a ComponentInternalInstance
class object that represents an internal example of the component and contains some properties for advanced applications, including VNode
.