Skip to content

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

Closed

Description

@hcg1023

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

Activity

  1. jacekkarczmarczyk commented on Jan 13, 2022

    @jacekkarczmarczyk
    Contributor

    Arrow function seems to work

  2. hcg1023 commented on Jan 13, 2022

    @hcg1023
    ContributorAuthor

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

  3. jacekkarczmarczyk commented on Jan 13, 2022

    @jacekkarczmarczyk
    Contributor
  4. hcg1023 commented on Jan 13, 2022

    @hcg1023
    ContributorAuthor

    example

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

  5. LinusBorg commented on Jan 13, 2022

    @LinusBorg
    Member

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

  6. hcg1023 commented on Jan 14, 2022

    @hcg1023
    ContributorAuthor

    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

  7. LinusBorg commented on Jan 14, 2022

    @LinusBorg
    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.

  8. added
    🐞 bug
    has workaround
    on Jan 14, 2022
  9. hcg1023 commented on Mar 23, 2022

    @hcg1023
    ContributorAuthor

    I created a PR for this problem #5615

  10. edison1105 commented on Sep 19, 2026

    @edison1105
    Member

    The function-parameter generation bug was fixed by #12823 (b0a1f05). Rechecked the original method default and the variants from the comments, including the this: undefined annotations, with Vue 3.5.43 (4ab865a): all six compiled callbacks retain their argument and return TEST for test. The method variants lose their argument in the Vue 3.2.26 control. The declarations also pass TypeScript 5.6.3 and 6.0.3, while an invalid numeric argument is still rejected.

    The original Playground also places const msg = ref('') outside <script setup>; that declaration needs to be inside the script block to run the template. This is separate from the compiler bug above. Closing as the reported parameter-loss issue is fixed.

  11. locked and limited conversation to collaborators on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐞 bughas workaroundscope: script-setup🔩 p2-edge-case

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions