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

Typescript props type inference is incorrect #15

Closed
wcldyx opened this issue Jun 28, 2019 · 4 comments
Closed

Typescript props type inference is incorrect #15

wcldyx opened this issue Jun 28, 2019 · 4 comments
Labels
enhancement New feature or request

Comments

@wcldyx
Copy link

wcldyx commented Jun 28, 2019

The type of msg should be string, but is inferred to be of type StringConstructor

import { createComponent } from 'vue-function-api'

    const MyComponent = createComponent({
        props: {
            msg: String
        },
        setup(props) {
            console.log(props.msg == '1'); // string | undefined
           // Error:(13, 25) TS2367: This condition will always return 'false' since the types 'StringConstructor' and '"1"' have no overlap.
        }
    })
@jsmith
Copy link

jsmith commented Jul 3, 2019

The PR from @ChuChencheng works great!

There are still issues with the required option. Here is an example from the RFC:

import { createComponent } from 'vue'

createComponent({
  props: {
    foo: {
      type: String,
      required: true,
    },
    bar: {
      type: String,
    },
  } as const,
  setup(props) {
    props.foo // "string" as expected
    props.bar // should be -> "string | undefined" but it actually "string"
  }
})

@beeplin
Copy link

beeplin commented Jul 17, 2019

@liximomo Do we have any plan to merge #16 and fix the required issue mentioned above by @jsmith ?

@IlCallo
Copy link

IlCallo commented Jul 22, 2019

Trying it out as well, found some problems with props inference too.

props: {
    // complexObject: {
    //   type: (Object as unknown) as PropType<ComplexObject>,
    //   required: true,
    // }, <= inferred as  {type: ComplexObject, required true}, should be inferred as ComplexObject
    complexObject: (Object as unknown) as PropType<ComplexObject>, // <= inferred as ComplexObject, but there is no required, should be inferred as optional
  } as const,

In general, this section doesn't seem to be enforced in the actual implementation.

PS: remember to import PropType from vue-function-api, it has a different signature from the one into vue

PS2:

In 3.0, the props declaration is optional. If you don't want runtime props validation, you can omit props declaration and declare your expected prop types directly in TypeScript

If anyone is wondering, this isn't already working (as expected I guess)

@posva posva added the enhancement New feature or request label Jul 31, 2019
@pauloevpr
Copy link

pauloevpr commented Aug 15, 2019

I have the same issue. Type inference does not work properly for props:

image

image

My workaround to avoid Typescript compilation issues is to use setup(props: any) {} to skip inference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants