TYPESCRIPT: 函数缺少结束 return 语句并且 return 类型不包括 'undefined'
TYPESCRIPT: Function lacks ending return statement and return type does not include 'undefined'
新手这里有一些丑陋的代码。我仍在研究方法封装。我正在尝试编写允许我比较两个输入字符串和 return 其“anagram”状态的布尔值的代码,条件如下所示。如果有人可以为我提供解决方案或解决方法来解决“函数缺少结束 return 语句并且 return 类型不包括未定义”,我将不胜感激。欢迎和赞赏任何建议。提前致谢!
class Example {
firstWord = prompt();
secondWord = prompt();
public isAnAnagram(firstWord: string, secondWord: string): boolean {
if (firstWord && secondWord !== null) {
const firstArray = firstWord.split("");
const secondArray = secondWord.split("");
// Show how the firstword and secondword have transformed into arrays
console.log(firstArray, secondArray);
let arrayPassed = true;
if (
firstArray.every((w) => secondArray.includes(w)) &&
secondArray.every((w) => firstArray.includes(w))
) {
// Show if first word passes anagram test through console
console.log("Found all letters of " + firstWord + " in " + secondWord);
} else {
arrayPassed = false;
// Show if first word does not pass anagram test through console
console.log(
"Did not find all letters of " + firstWord + " in " + secondWord
);
}
return arrayPassed;
}
}
}
您可以尝试在 return 之前使用 try catch。
你通过检查这个开始你的功能:
if (firstWord && secondWord !== null) {
if return 中的代码是一个布尔值,但是没有 else
块,if
之后也没有代码。因此,如果代码与 if
不匹配,您将隐式 returning undefined
。这与您告诉 typescript 的 return 类型相矛盾:boolean
.
public isAnAnagram(firstWord: string, secondWord: string): boolean
要解决此问题,请添加 else
案例和 return 布尔值:
else {
return false
}
或更改 return 类型以允许您 return undefined
。
public isAnAnagram(firstWord: string, secondWord: string): boolean | undefined
新手这里有一些丑陋的代码。我仍在研究方法封装。我正在尝试编写允许我比较两个输入字符串和 return 其“anagram”状态的布尔值的代码,条件如下所示。如果有人可以为我提供解决方案或解决方法来解决“函数缺少结束 return 语句并且 return 类型不包括未定义”,我将不胜感激。欢迎和赞赏任何建议。提前致谢!
class Example {
firstWord = prompt();
secondWord = prompt();
public isAnAnagram(firstWord: string, secondWord: string): boolean {
if (firstWord && secondWord !== null) {
const firstArray = firstWord.split("");
const secondArray = secondWord.split("");
// Show how the firstword and secondword have transformed into arrays
console.log(firstArray, secondArray);
let arrayPassed = true;
if (
firstArray.every((w) => secondArray.includes(w)) &&
secondArray.every((w) => firstArray.includes(w))
) {
// Show if first word passes anagram test through console
console.log("Found all letters of " + firstWord + " in " + secondWord);
} else {
arrayPassed = false;
// Show if first word does not pass anagram test through console
console.log(
"Did not find all letters of " + firstWord + " in " + secondWord
);
}
return arrayPassed;
}
}
}
您可以尝试在 return 之前使用 try catch。
你通过检查这个开始你的功能:
if (firstWord && secondWord !== null) {
if return 中的代码是一个布尔值,但是没有 else
块,if
之后也没有代码。因此,如果代码与 if
不匹配,您将隐式 returning undefined
。这与您告诉 typescript 的 return 类型相矛盾:boolean
.
public isAnAnagram(firstWord: string, secondWord: string): boolean
要解决此问题,请添加 else
案例和 return 布尔值:
else {
return false
}
或更改 return 类型以允许您 return undefined
。
public isAnAnagram(firstWord: string, secondWord: string): boolean | undefined