Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue props type with TypeScript

When a prop type is Number, String or Boolean, it will given the corrent type promty:

enter image description here

but when prop type is Object, it will be an any type:

enter image description here

and when i cast the Object to a function that returns the interface you’d like, it will be given the corrent type also:

enter image description here

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?

like image 287
jkchao Avatar asked Jul 02 '18 04:07

jkchao


People also ask

Does Vue work well with TypeScript?

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.

Does vue2 support TypeScript?

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.

What is defineComponent in vue3?

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.

Does Vue 3 support TypeScript?

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.


1 Answers

This is so-called constructor type.

More strictly, this property can appear in following different ways:

  • as a generic function (): T returning the specified type T;
  • as a generic constructor 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.

like image 80
Cerberus Avatar answered Sep 24 '22 04:09

Cerberus