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

Add info for wrapping refrence types in functions for withDefaults #2909

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

Topograph
Copy link
Contributor

This code in typescript triggers a confusing Error:

export interface Props {
  msg?: string
  labels?: string[]
}

const props = withDefaults(defineProps<Props>(), {
  msg: 'hello',
  labels: ['one', 'two'] 
})

Type 'string[]' is not assignable to type '(props: LooseRequired) => string[]'.
Type 'string[]' provides no match for the signature '(props: LooseRequired): string[]'.ts-plugin(2322)
TestComp.vue(7, 3): The expected type comes from property 'labels' which is declared here on type 'InferDefaults<LooseRequired>'

Description of Problem

This is correctly circumvented in the example in the docs by wrapping the default value for "labels" in a function.

export interface Props {
  msg?: string
  labels?: string[]
}

const props = withDefaults(defineProps<Props>(), {
  msg: 'hello',
  labels: () => ['one', 'two'] // wrapped in function
})

However its not clearly explained and the error message is confusing

Proposed Solution

I feel like adding a short explanation to the docs as proposed in the pull request might clear things up.

Copy link

netlify bot commented Jun 27, 2024

Deploy Preview for vuejs ready!

Name Link
🔨 Latest commit e206ae0
🔍 Latest deploy log https://app.netlify.com/sites/vuejs/deploys/667d2773908d450008513401
😎 Deploy Preview https://deploy-preview-2909--vuejs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@NataliaTepluhina NataliaTepluhina merged commit 6da06fb into vuejs:main Jul 9, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants