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

types(defineComponent): props support in TypeScript #3798

Closed

Conversation

Zclhlmgqzc
Copy link
Contributor

fix #3796

@hairyf

This comment has been minimized.

@Zclhlmgqzc Zclhlmgqzc force-pushed the types/support_defineComponent_props branch 2 times, most recently from 6aeef21 to 0386adf Compare August 25, 2021 10:29
@pikax
Copy link
Member

pikax commented Aug 25, 2021

@Zclhlmgqzc can you fix test-dts errors?

to test locally run yarn test-dts

@Zclhlmgqzc Zclhlmgqzc force-pushed the types/support_defineComponent_props branch from 0386adf to 498fb0b Compare August 25, 2021 11:01
@Zclhlmgqzc
Copy link
Contributor Author

@Zclhlmgqzc can you fix test-dts errors?

to test locally run yarn test-dts

Which code is better?

export type DefineComponent<
...
  - Props = Readonly<ExtractPropTypes<PropsOrPropOptions>> & 
  + Props = Readonly<ExtractPropTypes<PropsOrPropOptions extends undefined ? {} : PropsOrPropOptions>> & 
...
>

or

export type ExtractPropTypes<O> = O extends object
  ? { [K in keyof O]?: unknown } & // This is needed to keep the relation between the option prop and the props, allowing to use ctrl+click to navigate to the prop options. see: #3656
      { [K in RequiredKeys<O>]: InferPropType<O[K]> } &
      { [K in OptionalKeys<O>]?: InferPropType<O[K]> }
 + : O extends undefined
 + ? {}
  : { [K in string]: any }

@pikax
Copy link
Member

pikax commented Aug 25, 2021

Which code is better?

Can't you just not pass undefined on the generic?

@Zclhlmgqzc
Copy link
Contributor Author

Zclhlmgqzc commented Aug 25, 2021

Which code is better?

Can't you just not pass undefined on the generic?

because

DefineComponent =
...
& {
    props: PropsOrPropOptions
  }

const Comp = defineComponent({ props: {} })

==> {
 props: {}
}

//  Comp.props === {}

const Comp = defineComponent({})

==> {
 props: undefined
}

// Comp.props === undefined
// @ts-expect-error
Comp.props.a

@pikax
Copy link
Member

pikax commented Aug 25, 2021

Thanks, maybe the second option might be better (but just a preference)

@Zclhlmgqzc
Copy link
Contributor Author

Thanks, maybe the second option might be better (but just a preference)

Thanks for your review

@Zclhlmgqzc
Copy link
Contributor Author

see #5416

@Zclhlmgqzc Zclhlmgqzc closed this Feb 12, 2022
@Zclhlmgqzc Zclhlmgqzc deleted the types/support_defineComponent_props branch May 6, 2022 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

typing defineComponent props
3 participants