3

I use Stimulus in Symfony 5 and when I use this code in my Stimulus controller, PhpStorm shows an error.

How can I fix this?

import {Controller} from 'stimulus';
import {index} from "../js/index";
export default class extends Controller{
    connect(){
        const index1 = index();
    }
}

Error: Void function return value is used

My index() function is:

export function index(){
...
}
LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • 1
    Does index return anything? If not, you can just remove `const index1 = `, because it will not set anything when you call `index()` anyway, so there is nothing to assign – dbrumann Apr 21 '21 at 05:49

1 Answers1

-1

Use this code:

import {Controller} from 'stimulus';
import Index from "../js/index";
export default class extends Controller{
    connect(){
        const index1 = new Index;
        index1.index();
    }
}

And use this for your index class:

export default class Index {
    index(){
        ...
    }
}
  • 1
    Wouldn't it be much simpler replace `const index1 = index();` with `index();`? Why is a class necessary? – Felix Kling Apr 21 '21 at 12:58
  • @FelixKling When you create a class and want to use in other class, you need to use `const index1 = new Index` . –  Apr 21 '21 at 15:25
  • 1
    All I see that the user wants to call a function, not actually use a class. But maybe I don't understand what this is all about. – Felix Kling Apr 21 '21 at 20:45
  • 3
    @BahmanMD please can you explain why yours works? – Eoin May 28 '21 at 13:04