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

The withDefaults function loses arguments when assigning a function default value #5253

Open
hcg1023 opened this issue Jan 13, 2022 · 8 comments · May be fixed by #5615
Open

The withDefaults function loses arguments when assigning a function default value #5253

hcg1023 opened this issue Jan 13, 2022 · 8 comments · May be fixed by #5615
Labels
has workaround A workaround has been found to avoid the problem 🐞 bug Something isn't working 🔩 p2-edge-case scope: script-setup

Comments

@hcg1023
Copy link
Contributor

hcg1023 commented Jan 13, 2022

Version

3.2.26

Reproduction link

sfc.vuejs.org/

Steps to reproduce

const props = withDefaults(defineProps<{
  fn?: (name: string) => void
}>(), {
  fn(name: string) {
    return name.toUpperCase()
  }
})
console.log(props.fn)

What is expected?

Returns the correct default value

What is actually happening?

Function defaults lose arguments

@jacekkarczmarczyk
Copy link
Contributor

Arrow function seems to work

@hcg1023
Copy link
Contributor Author

hcg1023 commented Jan 13, 2022

Arrow function seems to work
Can you give me a simple example? It doesn't seem to work if I try
sfc.vuejs.org/

@jacekkarczmarczyk
Copy link
Contributor

example

@hcg1023
Copy link
Contributor Author

hcg1023 commented Jan 13, 2022

example

Thank you, but why doesn't my way of writing it work? I think it's still a bug

@LinusBorg
Copy link
Member

When using a method like you do, you would have to explicitly type this: undefined as the first argument of the method.

@hcg1023
Copy link
Contributor Author

hcg1023 commented Jan 14, 2022

When using a method like you do, you would have to explicitly type this: undefined as the first argument of the method.

I specified this: undefined as you said, but it still does not work properly. I have tried three ways of writing, maybe you need to teach me

sfc.vuejs.org

@LinusBorg
Copy link
Member

The mentioned fact that you need to use an arrow function or annotate this properly is documented here:

https://v3.vuejs.org/guide/typescript-support.html#annotating-props

But indeed, a method with this: undefined doesn't work work on withDefaults(). But it does with arrow functions. Not sure if this is a necessary consequence of the TS limitation mentioned in the docs or we can fix it.

@LinusBorg LinusBorg added 🐞 bug Something isn't working scope: script-setup has workaround A workaround has been found to avoid the problem 🔩 p2-edge-case labels Jan 14, 2022
@LinusBorg LinusBorg reopened this Jan 14, 2022
hcg1023 added a commit to hcg1023/core that referenced this issue Mar 23, 2022
@hcg1023
Copy link
Contributor Author

hcg1023 commented Mar 23, 2022

I created a PR for this problem #5615

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has workaround A workaround has been found to avoid the problem 🐞 bug Something isn't working 🔩 p2-edge-case scope: script-setup
Projects
None yet
3 participants