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

prop with required: true have type undefined #106

Closed
kevinmarrec opened this issue Aug 29, 2019 · 9 comments
Closed

prop with required: true have type undefined #106

kevinmarrec opened this issue Aug 29, 2019 · 9 comments
Labels
documentation Improvements or additions to documentation

Comments

@kevinmarrec
Copy link

kevinmarrec commented Aug 29, 2019

Description

As Vue handle required validation and throws an error if a component is missing a required prop, the resulting type of a required prop shouldn't be "possibly" undefined.

Markup

import { createComponent } from '@vue/composition-api'

export default createComponent({
  props: {
    defaultNumber: {
      type: Number,
      default: 2
    },
    requiredNumber: {
      type: Number,
      required: true
    }
  },
  setup ({ defaultNumber, requiredNumber }) {
    return {
      defaultNumber,
      requiredNumber
    }
  }
})

Prop with default value 👍

image

Prop required

image
requiredNumber is expected to be of type number, not number | undefined

Notes

I guess there's something wrong around https://github.com/vuejs/composition-api/blob/master/src/component/componentProps.ts#L20

Workaround

@lbssousa mentionned casting props to const and it works.
But I think it shouldn't be needed and there is something to fix around types.

@lbssousa
Copy link

You may need to put as const at the end of props: { ... }

@kevinmarrec
Copy link
Author

kevinmarrec commented Aug 29, 2019

@lbssousa Nice catch ! but isn't there an issue around the types (see Notes section) that would make it work without having casting to const ?

Also, I'm wondering why we don't have Intellisense for type, default, required and validator like we used to with Vue.extend :
image

@liximomo
Copy link
Member

@kevinmarrec Why did it not work for me? 🤔

image

@kevinmarrec
Copy link
Author

@liximomo Which installed version of TypeScript have you in the workspace of this snippet ?

@kevinmarrec
Copy link
Author

kevinmarrec commented Aug 30, 2019

And about the original creation of this issue, is there anything we can fix instead of casting props to const ?

I mean it's working for props with default values so I still think there's some fix to do around types.

@liximomo
Copy link
Member

liximomo commented Sep 2, 2019

@kevinmarrec The as const is required. We need to use as const to narrow down the inferred type from boolean to true.

@liximomo liximomo added the documentation Improvements or additions to documentation label Sep 2, 2019
@liximomo liximomo changed the title prop with required: true shouldn't have type undefined prop with required: true have type undefined Sep 2, 2019
@liximomo liximomo closed this as completed Sep 2, 2019
@liximomo
Copy link
Member

In fact, we can get rid of as const.

@liximomo liximomo reopened this Sep 17, 2019
@kevinmarrec
Copy link
Author

Nice :)

@bbugh
Copy link

bbugh commented Sep 17, 2019

In case anyone else like me ends up here from a web search wondering about props with an inferred any, or missing intellisense for createComponent like mentioned above, note the readme states:

This plugin requires TypeScript version >3.5.1. If you are using vetur, make sure to set vetur.useWorkspaceDependencies to true.

https://github.com/vuejs/composition-api#typescript

Upgrading our TypeScript fixed the intellisense and type inference.

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

No branches or pull requests

4 participants