Literal constructor
This warning category is spelled [literal-constructors]
by qmllint.
Do not use function as a constructor
What happened?
A literal construction function was used as a constructor.
Why is that bad?
Calling a literal construction function such as Number
as a regular function coerces the passed value to a primitive number. However, calling Number
as a constructor returns an object deriving from Number
containing that value. This is wasteful and likely not the expected outcome. Moreover, it may lead to unexpected or confusing behavior because of the returned value not being primitive.
Example
import QtQuick Item { function numberify(x) { return new Number(x) } Component.onCompleted: { let n = numberify("1") console.log(typeof n) // object console.log(n === 1) // false if (new Boolean(false)) // All objects are truthy! console.log("aaa") // aa } }
To fix this warning, do not call these functions as constructors but as regular functions:
import QtQuick Item { function numberify(x) { return Number(x) } Component.onCompleted: { let n = numberify("1") console.log(typeof n) // number console.log(n === 1) // true if (Boolean(false)) console.log("aaa") // <not executed> } }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.