7

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?

1
  • You have a typo in required Commented Jul 23, 2019 at 21:37

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thinks a lot, but i still have the question: how does it infer the type i need? And an other question, when i cast the Object to a interface, Object as User it will be error. Type 'ObjectConstructor' cannot be converted to type 'User'. Property 'id' is missing in type 'ObjectConstructor'. it makes me wonder. why? Thinks again.
You don't have to cast Object as User. If it's possible to make a class User instead of interface User (i.e. if it won't have a significant overhead - and I assume it won't), you can just write propA: {type: User}, without using Object at all. I think I'll update my answer a bit later, to give possibly a more verbose clarification.
Maybe i understand for the first, if the prop type is function, accronding to the type Prop<T> = { (): T } | { new (...args: any[]) => T & object }, it will create object that the properties is function return value. In the question, the properties is User, Yes?
Right. You have the object of type () => User, as you've said explicitly by the as keyword. It fits into the first template: { () => T } (since function is just an object with call signature); with T = User. So, Prop<T> infers to Prop<User>.
There are some question about it, please look at it stackoverflow.com/questions/51296287/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.