Skip to content

Reactive Props Destructure: Prop is not defined outside a for...of loop that uses the same variable name #14294

@rak-phillip

Description

@rak-phillip

Vue version

3.5.26

Link to minimal reproduction

https://play.vuejs.org/#eNrNUlFv2jAQ/itXv5hKKKjrtgcWkBjKViYK1UDaJIwmFC40XWJHtsOQovz3nR0IbOv6OO0lyt333d3nu69io6II9iWyPgtNrNPCgkFbFkMh07xQ2kIFGhOoIdEqB05U3kJjlRfHfNBzgetEsJCxksZCbnYwcOUdfodZpuCL0tn2il8LGfaaaTSHAot5kW0sUgQQ+rZUOxDsokww6KeyKC2lV3wxv494F/h9tFiMPkZ8TXCPysPeRS/WZdaQlCTdBU9GSXpk5SYIFtOINEM9L2xKUgXrg0cctqGRPz75nNUldk/5+BHj78/kn8zB5QR70GhQ71GwFrMbvUPbwNFihgf6b8FcbcuM2C+An9GorHQaG9r7Um5J9gXPq534e6RytzTRwaI0p0c5oY5Ze75gdB+33b89/Sz3Nnjt64SsaYun2/7uEsg20p3JUhvnGGlRJ5sY4UGrwjR96ZB9MFaTuncu9jc8ZVZrytGQk2Oqo2f4XTSdzq/owp5OmRWfTcb+5NHXMYHRbMnXZMsBbDFJJfqJof8OO9fUVchEaejA2YoqOXa7bpQ5RGUYZGrXaY3UdVRX36pqKbP5Ej6MJlPiNDprx3vJyY83w+pIrcMeRb8Y9LjYm/92sxXU50X+ucfn1nixo3+5x2971M70tMbb4E3w6i2rfwJ+CZkN

Steps to reproduce

In the minimal reproduction example:

  1. Destructure variables from defineProps: const { msg = 'HELLO!', input = ['NICE', 'EXCELLENT'] } = defineProps<Props>();
  2. Declare a for...of loop that reuses a destructured prop name: for (const msg of input) { console.log('MESSAGE', msg); }
  3. After the loop, the original variable from props is now undefined

What is expected?

The component should execute without errors.

The console.log statement outside the loop should print without errors.

What is actually happening?

The script fails with an ReferenceError: msg is not defined at the console.log statement that occurs after the for...of loop.

System Info

System:
    OS: Linux 6.18 openSUSE Tumbleweed 20260101
    CPU: (24) x64 AMD Ryzen 9 9900X 12-Core Processor
    Memory: 29.93 GB / 60.43 GB
    Container: Yes
    Shell: 5.3.9 - /bin/bash
  Binaries:
    Node: 22.14.0 - /home/phillip/.asdf/installs/nodejs/22.14.0/bin/node
    Yarn: 1.22.22 - /home/phillip/.asdf/installs/nodejs/22.14.0/bin/yarn
    npm: 10.9.2 - /home/phillip/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chromium: 143.0.7499.169
    Firefox: 146.0.1
    Firefox Developer Edition: 146.0.1

Any additional comments?

Comp1.vue in the example demonstrates how this works in javascript without defineProps:

const { msg = 'HELLO!', input = ['NICE', 'EXCELLENT'] } = { };

for (const msg of input) {
  console.log('NOT FAIL', msg);
}

console.log('NOT FAIL', { msg });

Metadata

Metadata

Assignees

No one assigned

    Labels

    🔨 p3-minor-bugPriority 3: this fixes a bug, but is an edge case that only affects very specific usage.scope: sfc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions