느슨한 평등 비교 유형 강제
이 경고 카테고리의 철자는 [equality-type-coercion] 입니다.
느슨한 평등 비교 유형 강제
무슨 일이 일어났나요?
느슨한 비교 연산자를 사용하여 두 값이 같은지 비교했습니다.
이것이 왜 나쁜가요?
느슨한 비교 연산자는 동일성 여부를 확인하기 전에 값을 다른 유형으로 강제할 수 있습니다. 이로 인해 예기치 않은 결과가 발생할 수 있습니다.
예제
다음은 직사각형 목록과 TextInput. 사용자가 숫자를 입력하면 해당 인덱스의 직사각형이 강조 표시되는 예제입니다. 이 코드에는 결함이 있습니다. 입력이 비어 있으면 "" == 0이므로 첫 번째 사각형이 빨간색으로 강조 표시됩니다.
import QtQuick Item { TextInput { id: input } Repeater { model: 3 Rectangle { // first rectangle is red on empty input color: input.text == index ? "red" : "blue" } } }
일반적으로 엄격한 비교 연산자 === 및 !== 을 사용합니다. 강제성을 알고 있더라도 명시적 형 변환과 엄격한 비교를 대신 사용하는 것이 좋습니다.
© 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.