Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p({ type: Number }) doesn't work. #21

Closed
whitetrefoil opened this issue Nov 28, 2016 · 10 comments
Closed

p({ type: Number }) doesn't work. #21

whitetrefoil opened this issue Nov 28, 2016 · 10 comments

Comments

@whitetrefoil
Copy link
Contributor

TS v2.1.1, av-ts v0.5.4

num: number = p({
  type: Number
})

cause

Error:(15, 3) TS2322:Type 'Number' is not assignable to type 'number'.
  'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.

Boolean is similar.
But p(Number) works.

@HerringtonDarkholme
Copy link
Owner

HerringtonDarkholme commented Nov 28, 2016

Hi, this is an intended behavior.

In TypeScript, Number has the type NumberConstructor, and p(Number) is a hard coded override resolving to number.

But for API like p({type: Number}), TypeScript will resolve it using NumberConstructor's construct signature, which resolves to Number. Note number and Number is not compatible.

Just skip the manual annotation, most things will be fine because TS will implicit convert number to Number and vice versa. Also, if you don't need other fields like p({type: Number, default: 123, requried: true}), prefer p(Number).

@whitetrefoil
Copy link
Contributor Author

whitetrefoil commented Nov 28, 2016 via email

@HerringtonDarkholme
Copy link
Owner

@whitetrefoil You can just skip manual annotation.

num = p({type: Number, default: 123}) should work.

@whitetrefoil
Copy link
Contributor Author

@HerringtonDarkholme This line is ok w/o annotation, but it will still be problem when I use these props somewhere else. In my current case, I'll using these props to initialize spin.js, but I'll got the same error when new Spinner({...}).

@HerringtonDarkholme
Copy link
Owner

Oh... I see it. But I'm afraid this is unfixable issue.
Try annotate like this
num: number = p<any>({type: Number, default: 123})

@whitetrefoil
Copy link
Contributor Author

Yup, I guess this is the only thing we can do for now… I'm already used to fix everything using … (:3」∠)

@whitetrefoil
Copy link
Contributor Author

Just found that code like

  @Prop show    = <boolean> p({ type: Boolean, default: false })
  @Prop width   = <number> p({ type: Number, default: DEFAULT_WIDTH })

works...

@HerringtonDarkholme
Copy link
Owner

HerringtonDarkholme commented Nov 29, 2016

It also uses type assertion. But clearly fewer key strokes!

I would also recommend using as in case you want to use tsc one day

  @Prop show    = p({ type: Boolean, default: false }) as boolean

Thanks for the solution, I have collected it in FAQ!

@HerringtonDarkholme
Copy link
Owner

Hi @whitetrefoil

In case you are still interested, av-ts @0.8.1 shipped a new implementation of p.

Now @Prop show = p({type: Boolean, default: false}) does not need explicit casting!

@whitetrefoil
Copy link
Contributor Author

Nice! Will try the new version tomorrow~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants