8000 setup function provide a wrong prop type,when use type: Function · Issue #971 · vuejs/composition-api · GitHub
[go: up one dir, main page]

Skip to content

setup function provide a wrong prop type,when use type: Function #971

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

Closed
workkk98 8000 opened this issue Oct 20, 2022 · 3 comments
Closed

setup function provide a wrong prop type,when use type: Function #971

workkk98 opened this issue Oct 20, 2022 · 3 comments

Comments

@workkk98
Copy link
Contributor
workkk98 commented Oct 20, 2022

Dependencies

vue version: 2.6.14
@vue/composition-api: 1.7.1(latest)

background

we all know vue2.x prop check support Function.
image

like this case,Function type infer to never

import { defineComponent, onBeforeMount } from '@vue/composition-api'

export default defineComponent({
  props: {
    handler: {
      type: Function,
      required: true
    }
  },
  setup(props) {
    onBeforeMount(() => {
      // ts tell me handler type is never, but actually it is a function
      props.handler()
    })
  }
})
@workkk98
Copy link
Contributor Author

this case can explain by next link.It seems to ExtractFunctionPropType type cause this case.So maybe we should rewrite ExtractFunctionPropType like this.

declare type ExtractFunctionPropType2<T extends Function, TArgs extends Array<any> = any[], TResult = any> = T extends (...args: TArgs) => TResult ? T :  T extends Function ? (...args: any[]) => any : never;

ts playground

@workkk98
Copy link
Contributor Author

or we should add a union type in inferPropType like this.

type InferPropType<T> = T extends null
  ? any // null & true would fail to infer
  : T extends { type: null | true }
    ? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`
    : T extends ObjectConstructor | { type: ObjectConstructor }
      ? Record<string, any>
      : T extends BooleanConstructor | { type: BooleanConstructor }
        ? boolean
          : T extends DateConstructor | { type: DateConstructor}
            ? Date
              : T extends FunctionConstructor | { type: FunctionConstructor }
                ? Function
                : T extends Prop<infer V, infer D>
                  ? unknown extends V
                    ? D extends null | undefined
                      ? V
                      : D
                    : ExtractCorrectPropType<V>
                  : T

@github-actions
Copy link

Stale issue message

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

1 participant
0