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

fix(types): RequiredKeys should include Prop<T, D> #2483

Closed
wants to merge 3 commits into from

Conversation

KaelWD
Copy link
Contributor

@KaelWD KaelWD commented Oct 25, 2020

I have a factory function for props that uses Prop<T, D>, but when used in a component the props always include undefined even if D is set. Not entirely sure how to test this, but it works with vuetifyjs/vuetify#12510 when replaced in node_modules.

Example code:

const makeProps = propsFactory({
  tag: String,
})

defineComponent({
  props: makeProps(),

  setup (props) {
    props.tag // string | undefined
  },
})

defineComponent({
  props: makeProps({ tag: 'a' }),

  setup (props) {
    props.tag // should be string, actually string | undefined
  },
})

@pikax
Copy link
Member

pikax commented Oct 27, 2020

Can you add a test case for that?

@KaelWD
Copy link
Contributor Author

KaelWD commented Oct 28, 2020

Another way to test it is

type NoNullableFields<T> = { [P in keyof T]-?: NonNullable<T[P]> }

type Expected = { foo: boolean }
type Result = ExtractPropTypes<NoNullableFields<ComponentObjectPropsOptions<Expected>>>

expectType<{ foo: boolean }>({} as Result)

Basically making it possible to convert between Props and ComponentObjectPropsOptions without losing any type information.

@HcySunYang
Copy link
Member

@KaelWD Hey, can you fix the conflict?

@KaelWD
Copy link
Contributor Author

KaelWD commented Apr 1, 2021

Done.

@hulkyhawk
Copy link

@KaelWD if you use Prop type to default props instead of an object literal, then Vue cannot guarantee there is a default field then;

 const props: { foo: Prop<boolean> } = { foo: { type: Boolean } }

@KaelWD
Copy link
Contributor Author

KaelWD commented Sep 29, 2021

Not sure what you mean by that, Boolean props default to false

@hulkyhawk
Copy link

hulkyhawk commented Sep 30, 2021

Not sure what you mean by that, Boolean props default to false

i mean if you use object literal to make ts infer the type, then vue will know that if you have a default field in the PropOption. use Prop type will loose the information, since default is optional

sorry, boolean is not proper here, you can use Prop to do the following
const props: { foo: Prop<string> } = { foo: { type: String } }

@KaelWD KaelWD closed this Jun 8, 2023
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.

None yet

4 participants