When a prop type is Number, String or Boolean, it will given the corrent type promty:
but when prop type is Object, it will be an any type:
and when i cast the Object to a function that returns the interface you’d like, it will be given the corrent type also:
I find the prop type declaration in options.d.ts
export type Prop<T> = { (): T } | { new (...args: any[]) => T & object }
But i do not know what mean is it, and how does it infer the type?
Now, you should be able to get your Vue app up and running in TypeScript with features like defineComponent , data, props, computed properties, methods, and watchers. Vue 3.0 includes better support for TypeScript out of the box, and the entire Vue code was rewritten in TypeScript to improve maintainability.
Vue 2 already has good support for TypeScript, and the recently published Vue 2.7 backported a lot of useful features from Vue 3, like composition API, <script setup> , and defineComponent , further improving the developer experience of TypeScript in Vue.
defineComponent({ setup: function , name: 'some name' }); As you see, all these cases are responsible for different scenarios, yet the return type is always the same: a defineComponent interface with all the types you need to apply for the props, data and the other settings that are needed for the component.
Vue is written in TypeScript itself and provides first-class TypeScript support. All official Vue packages come with bundled type declarations that should work out-of-the-box.
This is so-called constructor type.
More strictly, this property can appear in following different ways:
(): T
returning the specified type T
;new (...args): T & object
, creating the object of specified type T
with additional properties from type object
.The Object
type satisfies the second variant, i.e. it's a class with some constructor property. It has the following definition (from lib.es5.d.ts
):
interface Object {
/** The initial value of Object.prototype.constructor is the standard built-in Object constructor. */
constructor: Function;
// some more properties here
}
Note that this is the most general constructor possible, and so it could potentionally return any possible value. So the transpiler tells you exactly that - "you have a class whose constructor returns any
".
Seems that you could just use User
as property type. Since it's a class too, I suppose, it will have the signature of new (...args) => User
, so it would infer exactly the type you need.
upd: missed the interface definition in the question. Well, if there's no reason to make it a class instead, the proper typing (and usage) seems to be the function: () => User
, not Object
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With