Subscribe on changes!

Script Setup - Class constructor [class] cannot be invoked without 'new'

avatar
Aug 16th 2022

Vue version

3.2.37

Link to minimal reproduction

https://sfc.vuejs.org/#eNp9kMtOw0AMRX/FGiGlldrMvgRE1Q0fwHI26cSBlMxDHqcFRfl3nAelAond+F7Psa97tY8xP3eodqpIlprIkJC7+Gh842Ighh4IaxigpuAgk9bsar1g4kNbprSYuWZR7Kjkp5TdG1/oGSo4KRhdbEtGqQCKY8ccPDzZtrHvD0bdoWt4lbnPScg24PHyM2K1Xhs1fQR4xrYNE0PPENELmb3Q1UbNC25dGWWR4CVdP/abxUhG7WBSRk0yjbVRb8wx7bROtR1vckp5oFctr5w6z43DHJPbHilcEpKAjdrcMLSIZ6Qtoa+QkP5j/mr9wx2xg/GDRLk9qeTAj+n0FdZl1zJMDuyr6hCcK301p7LBJ6bOcqDV+hr0mzl8ARwjrgw=

Steps to reproduce

  1. Open link to SFC Playground
  2. Click the button
  3. An error Class constructor [class] cannot be invoked without 'new' is raised

What is expected?

No error should be visible in <script setup> as the the equivalent behavior is already working in <script>

What is actually happening?

The code is raising an error Class constructor [class] cannot be invoked without 'new' is raised

System Info

No response

Any additional comments?

See Discussion: https://github.com/vuejs/core/discussions/6480

avatar
Aug 16th 2022

The compiler produces this code:

(_ctx.$emit('myclick', new _unref(TestClass)())

which is invalid. This should be fixable with an additional set of paranthesis if we need to unwrap directly after new:

(_ctx.$emit('myclick', new (_unref(TestClass))())
avatar
Jul 31st 2023

Guys, any update on this? @yyx990803 It seems multiple same issues happens nowadays, and it is not a rare case, can you reevaluate this issue?

avatar
Nov 8th 2023

Any update on this issue? Does that mean one should avoid using classes within script/setup? Is there a workaround?

avatar
Dec 29th 2023

Is it possible to have a follow up on this issue ? The fact that it impacts only the code pushed in production, and not the code in dev mode is especially hurting :'(.

For reference I've been bitten by it both for bindings :var="new Test()" and triggers @click="test = new Test()"

It is sometimes lighter and more readable to be able to new something directly in the template, but if it is not working in production I would largely prefer to break things in my IDE, rather than having to patch some production code somewhere.