このページでは

リテラルコンストラクタ

この警告カテゴリは、qmllint では「[literal-constructors] 」と表記されます。

関数をコンストラクタとして使用しないでください

何が起きたのか?

リテラルな構築関数がコンストラクタとして使用されました。

なぜ問題なのか?

Number のようなリテラル生成関数を通常の関数として呼び出すと、渡された値はプリミティブ数値に強制変換されます。しかし、Number をコンストラクタとして呼び出すと、その値を含むNumber を継承したオブジェクトが返されます。これはリソースの無駄であり、期待される結果ではないでしょう。さらに、返される値がプリミティブではないため、予期しない、あるいは混乱を招くような動作につながる可能性があります。

例

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
    }
}

この警告を解消するには、これらの関数をコンストラクタとしてではなく、通常の関数として呼び出してください:

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>
    }
}

Array のセマンティクスが紛らわしい

何が起きたのか?

Array のコンストラクタが、複数の引数とともに使用されました。

なぜ問題なのでしょうか?

Array のコンストラクタには、意味が分かりにくい挙動があります。

  • new Array() 空の配列を作成します。
  • new Array(42) 42個の要素を持つ配列を作成します。
  • new Array(42, 43) 要素が 42 と 43 の2つの要素を持つ配列を作成します。

例

import QtQuick

Item {
    function createArray() {
        return new Array(1, 2, 3)
    }
}

この警告を解消するには、new Array(...) の代わりに[...] を使用してください。

関数:

import QtQuick

Item {
    function createArray() {
        return [1, 2, 3]
    }
}

© 2026 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.