π« ERROR in [eslint]
Expected to return a value at the end of arrow function consistent-return
ESLintμ consistent-return λ¬Έμλ₯Ό μ°Έκ³ νμ΅λλ€!
consistent-return κ·μΉμ return κ°μ μΌκ΄λκ² μ μ§νλλ‘ νλ κ·μΉμ΄λ€.
ν¨μ λ΄λΆμμ λ°λ³΅λ¬Έμ΄λ 쑰건문μ μ¬μ©ν΄μ return νλ €κ³ νλ κ²½μ°μ νΉμ 쑰건μμλ κ°μ λ°ννμ§ μμμ return κ°μ΄ μΌκ΄λμ§ μμ μ μλ€.
μλ°μ€ν¬λ¦½νΈμμλ μλμ κ°μ΄ return κ°μ λͺ
μμ μΌλ‘ νμ§ μμΌλ©΄ undefinedλ₯Ό λ°ννλ€.
function doSomething(condition) {
if (condition) {
return true;
} else {
return; // undefinedλ₯Ό λ°νν¨
}
}
μ΄λ κ² λ°ν νμ μ μ μ μΌλ‘ μ§μ νμ§ μλ μλ°μ€ν¬λ¦½νΈμ νΉμ±μΌλ‘ μΈν΄μ λ€μν μ νμ κ°μ λ°ννλλ‘ νλ μ μ λ§κΈ° μν΄μ consistent-return κ·μΉμ μ¬μ©νλ€.
νΉμ μ‘°κ±΄μΌ λμλ κ°μ λ°ννμ§ μλ κ²½μ°μλ undefinedλ₯Ό λ°ννκΈ° λλ¬Έμ κ·μΉμ μ€μνμ§ μλ μ½λμ΄λ€.
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
function doSomethingElse(condition) {
if (condition) {
return true;
}
}
κ·μΉμ μ€μνλλ‘ μ½λλ₯Ό μμ νλ©΄ μλμ κ°λ€.
function doSomething(condition) {
if (condition) {
return true;
} else {
return false;
}
}
λΆλ μμ λ§€κ°λ³μμ null κ°μ΄ μ‘΄μ¬νλμ§ νμΈνλ ν¨μλ₯Ό μμ±νλ €κ³ νλ€.
const hasNull = (...args: (string | number | null)[]): boolean => {
args.forEach((arg) => {
if (arg === null) {
return true
}
})
return false
}
μμ²λΌ argκ° nullμΌ λλ trueλ₯Ό λ°ννκ³ argsλ₯Ό λͺ¨λ νμΈν νμλ falseλ₯Ό λ°ννλλ‘ μμ±νλ€.
κ·Έλ°λ° 쑰건문 ifμ elseμΈ κ²½μ°μ λν΄μλ return λ¬Έμ΄ μκΈ° λλ¬Έμ μλ¬κ° λ°μνλ€.
μ΄λ°μ λ° μλλ₯Ό νλ€κ° μκ°λ κ² some λ©μλμ΄λ€!
λ°°μ΄μ μμκ° νΉμ 쑰건μ λ§μ‘±νλμ§ μ¬λΆλ₯Ό νμΈν λ μ¬μ©νλ λ©μλμ΄κΈ° λλ¬Έμ λ± μ μ ν λ©μλμλ€.
κ·Έλμ μ½λλ₯Ό μλμ²λΌ μμ νλ€.
const hasNull = (...args: (string | number | null)[]): boolean => {
return args.some((arg) => arg === null)
}
javaλ‘ μ½ν νλ μμ²λΌ 쑰건문μ μ¬μ©ν΄μ return νλ λ°©μμ μμ£Ό μ¬μ©νλ€λ³΄λ μ΄λ κ² μ½λλ₯Ό μμ±νμλλ°, μλ°μ€ν¬λ¦½νΈλ‘ μ½λλ₯Ό μ§€ λλ μμ€μ νΉμ±μ λ§μΆ°μ consistent-return κ·μΉμ μ μ€μνλ κ² μ½λ νμ§μ λ μ’μ λ―νλ€!
κ·Έλ¦¬κ³ λ§€λ² μ°λ λ©μλ(foreach, map)λ§ μ°λ€λ³΄λ some, filter μ¬μ©μ κΉλ¨Ήκ² λλ€. μμ€ κ³΅λΆλ₯Ό μνν νμ§ λ§μ!π