将 JavaScript 函数重构为 TypeScript?
Refactor JavaScript function to TypeScript?
我刚开始学习打字稿(真的刚开始),我正试图让这个功能在运行这个测试时起作用:
import { convertNumberToEnglishText } from './index';
describe('Number to English converter test suite', () => {
it('should print zero for 0', () => {
expect(convertNumberToEnglishText(0)).toBe('zero');
});
it('should print zero for 0', () => {
expect(convertNumberToEnglishText(-0)).toBe('zero');
});
it('should print negative one for -1', () => {
expect(convertNumberToEnglishText(-1)).toBe('negative one');
});
it('should print nineteen for 19', () => {
expect(convertNumberToEnglishText(19)).toBe('nineteen');
});
it('should print twenty for 20', () => {
expect(convertNumberToEnglishText(20)).toBe('twenty');
});
it('should print correctly for 41', () => {
expect(convertNumberToEnglishText(41)).toBe('forty one');
});
it('should print correctly for 100', () => {
expect(convertNumberToEnglishText(100)).toBe('one hundred');
});
it('should print correctly for 101', () => {
expect(convertNumberToEnglishText(101)).toBe('one hundred one');
});
it('should print correctly for 739', () => {
expect(convertNumberToEnglishText(739)).toBe('seven hundred thirty nine');
});
it('should print correctly for 1234', () => {
expect(convertNumberToEnglishText(1234)).toBe('one thousand two hundred thirty four');
});
it('should print correctly for 10000', () => {
expect(convertNumberToEnglishText(10000)).toBe('ten thousand');
});
it('should print correctly for 60019', () => {
expect(convertNumberToEnglishText(60019)).toBe('sixty thousand nineteen');
});
it('should print correctly for 67567', () => {
expect(convertNumberToEnglishText(67567)).toBe('sixty seven thousand five hundred sixty seven');
});
it('should print correctly for 99999', () => {
expect(convertNumberToEnglishText(99999)).toBe('ninety nine thousand nine hundred ninety nine');
});
});
我在函数中遇到的唯一错误是第一行的第一个字符串类型声明。它说“函数缺少结束 return 语句并且 return 类型不包括 'undefined'。”
export function convertNumberToEnglishText(n: number): string {
const num = [
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
];
const tens = [
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
];
if (n <= 0) return "negative " + convertNumberToEnglishText(-n);
let digit: number = n % 10;
if (n >= 0 && n < 20) return `${num[n]}`;
if (n >= 20 && n < 100) return `${tens[Math.floor(n / 10) - 1]} ${digit != 0 ? num[digit] : ""}`;
if (n >= 100 && n < 1000) {
return `${num[Math.floor(n / 100)]} hundred ${
n % 100 == 0 ? "" : convertNumberToEnglishText(n % 100)
}`;
}
if (n >= 1000 && n < 10000) {
return `${num[Math.floor(n / 1000)]} thousand ${
n % 1000 != 0 ? convertNumberToEnglishText(n % 1000) : ""
}`;
}
if (n >= 10000 && n < 20000) {
return `${num[Math.floor(n / 1000)]} thousand ${
n % 1000 != 0 ? convertNumberToEnglishText(n % 1000) : ""
}`;
}
if (n >= 20000 && n < 100000) {
return `${tens[Math.floor(n / 10000) - 1]} ${
num[Math.floor(n / 10000)]
} thousand ${n % 1000 != 0 ? convertNumberToEnglishText(n % 1000) : ""}`;
}
}
此函数中的每个 return
语句都嵌套在一个条件分支中。这意味着,潜在地,该函数可能无法通过所有这些条件检查,并且不会执行任何 return 语句。这将导致函数 returning undefined
这是一个从未明确 return 值的函数的 return 值。如果您传入的值超过 100000
,您的函数就会发生这种情况,因为没有条件分支来处理它。
但是,函数的 return 类型被键入为 string
,这意味着 undefined
不是允许的 return 值。这就是错误的含义。
您需要定义当输入超出您的函数处理范围时会发生什么。有很多方法可以做到这一点,具体取决于您想要做什么。
您可以在函数的最后一行抛出错误。只有当所有其他条件子句都失败并且没有执行任何 return 语句时才会被命中。
throw new Error("Only values below 100,000 are supported")
您可以回退到仅将数字打印为最后一行的数字:
return n.toString()
或者您可以将 return 类型更改为 string | undefined
并在末尾添加一个 return
(没有值)。这告诉打字稿该函数可能 return undefined
并且该函数的调用者需要处理这种情况。
我刚开始学习打字稿(真的刚开始),我正试图让这个功能在运行这个测试时起作用:
import { convertNumberToEnglishText } from './index';
describe('Number to English converter test suite', () => {
it('should print zero for 0', () => {
expect(convertNumberToEnglishText(0)).toBe('zero');
});
it('should print zero for 0', () => {
expect(convertNumberToEnglishText(-0)).toBe('zero');
});
it('should print negative one for -1', () => {
expect(convertNumberToEnglishText(-1)).toBe('negative one');
});
it('should print nineteen for 19', () => {
expect(convertNumberToEnglishText(19)).toBe('nineteen');
});
it('should print twenty for 20', () => {
expect(convertNumberToEnglishText(20)).toBe('twenty');
});
it('should print correctly for 41', () => {
expect(convertNumberToEnglishText(41)).toBe('forty one');
});
it('should print correctly for 100', () => {
expect(convertNumberToEnglishText(100)).toBe('one hundred');
});
it('should print correctly for 101', () => {
expect(convertNumberToEnglishText(101)).toBe('one hundred one');
});
it('should print correctly for 739', () => {
expect(convertNumberToEnglishText(739)).toBe('seven hundred thirty nine');
});
it('should print correctly for 1234', () => {
expect(convertNumberToEnglishText(1234)).toBe('one thousand two hundred thirty four');
});
it('should print correctly for 10000', () => {
expect(convertNumberToEnglishText(10000)).toBe('ten thousand');
});
it('should print correctly for 60019', () => {
expect(convertNumberToEnglishText(60019)).toBe('sixty thousand nineteen');
});
it('should print correctly for 67567', () => {
expect(convertNumberToEnglishText(67567)).toBe('sixty seven thousand five hundred sixty seven');
});
it('should print correctly for 99999', () => {
expect(convertNumberToEnglishText(99999)).toBe('ninety nine thousand nine hundred ninety nine');
});
});
我在函数中遇到的唯一错误是第一行的第一个字符串类型声明。它说“函数缺少结束 return 语句并且 return 类型不包括 'undefined'。”
export function convertNumberToEnglishText(n: number): string {
const num = [
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
];
const tens = [
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety",
];
if (n <= 0) return "negative " + convertNumberToEnglishText(-n);
let digit: number = n % 10;
if (n >= 0 && n < 20) return `${num[n]}`;
if (n >= 20 && n < 100) return `${tens[Math.floor(n / 10) - 1]} ${digit != 0 ? num[digit] : ""}`;
if (n >= 100 && n < 1000) {
return `${num[Math.floor(n / 100)]} hundred ${
n % 100 == 0 ? "" : convertNumberToEnglishText(n % 100)
}`;
}
if (n >= 1000 && n < 10000) {
return `${num[Math.floor(n / 1000)]} thousand ${
n % 1000 != 0 ? convertNumberToEnglishText(n % 1000) : ""
}`;
}
if (n >= 10000 && n < 20000) {
return `${num[Math.floor(n / 1000)]} thousand ${
n % 1000 != 0 ? convertNumberToEnglishText(n % 1000) : ""
}`;
}
if (n >= 20000 && n < 100000) {
return `${tens[Math.floor(n / 10000) - 1]} ${
num[Math.floor(n / 10000)]
} thousand ${n % 1000 != 0 ? convertNumberToEnglishText(n % 1000) : ""}`;
}
}
此函数中的每个 return
语句都嵌套在一个条件分支中。这意味着,潜在地,该函数可能无法通过所有这些条件检查,并且不会执行任何 return 语句。这将导致函数 returning undefined
这是一个从未明确 return 值的函数的 return 值。如果您传入的值超过 100000
,您的函数就会发生这种情况,因为没有条件分支来处理它。
但是,函数的 return 类型被键入为 string
,这意味着 undefined
不是允许的 return 值。这就是错误的含义。
您需要定义当输入超出您的函数处理范围时会发生什么。有很多方法可以做到这一点,具体取决于您想要做什么。
您可以在函数的最后一行抛出错误。只有当所有其他条件子句都失败并且没有执行任何 return 语句时才会被命中。
throw new Error("Only values below 100,000 are supported")
您可以回退到仅将数字打印为最后一行的数字:
return n.toString()
或者您可以将 return 类型更改为 string | undefined
并在末尾添加一个 return
(没有值)。这告诉打字稿该函数可能 return undefined
并且该函数的调用者需要处理这种情况。