Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JavaScript函数重构为TypeScript?_Javascript_Typescript_Refactoring - Fatal编程技术网

将JavaScript函数重构为TypeScript?

将JavaScript函数重构为TypeScript?,javascript,typescript,refactoring,Javascript,Typescript,Refactoring,我刚开始学习typescript(实际上才刚刚开始),我正试图在运行此测试时使此函数正常工作: import { convertNumberToEnglishText } from './index'; describe('Number to English converter test suite', () => { it('should print zero for 0', () => { expect(convertNumberToEnglishTex

我刚开始学习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');
    });
});

我在函数中得到的唯一错误是第一行中的第一个字符串类型声明。它说“函数缺少结束返回语句,返回类型不包括‘undefined’。”

导出函数convertNumberToEnglishText(n:number):字符串{
常数num=[
“零”,
“一个”,
“两个”,
“三”,
“四”,
“五个”,
“六”,
“七”,
“八”,
“九”,
“十”,
“十一”,
“十二”,
“十三”,
“十四”,
“十五”,
“十六”,
“十七”,
“十八”,
“十九”,
];
常数十=[
“十”,
“二十”,
“三十”,
“四十”,
“五十”,
“六十”,
“七十”,
“八十”,
“九十”,
];
if(n=0&&n<20)返回`${num[n]}`;
如果(n>=20&&n<100)返回`${tens[Math.floor(n/10)-1]}${digit!=0?num[digit]:“”}`;
如果(n>=100&&n<1000){
返回`${num[Math.floor(n/100)]}100${
n%100==0?“:convertNumberToEnglishText(n%100)
}`;
}
如果(n>=1000&&n<10000){
返回`${num[Math.floor(n/1000)]}000${
n%1000!=0?convertNumberToEnglishText(n%1000):“”
}`;
}
如果(n>=10000&&n<20000){
返回`${num[Math.floor(n/1000)]}000${
n%1000!=0?convertNumberToEnglishText(n%1000):“”
}`;
}
如果(n>=20000&&n<100000){
返回`${tens[Math.floor(n/10000)-1]}${
数量[数学楼层(n/10000)]
}千${n%1000!=0?convertNumberToEnglishText(n%1000):“}”;
}
}

此函数中的每个
return
语句都嵌套在条件分支中。这意味着,该函数可能会使所有这些条件检查失败,并且不会执行任何return语句。这将导致函数返回
undefined
,这是从未显式返回值的函数的返回值。如果传入的值大于
100000
,则函数将发生这种情况,因为没有条件分支来处理该值

但是,函数的返回类型为
string
,这意味着
undefined
不是允许的返回值。这就是错误的含义

您需要定义当输入超出函数处理的边界时会发生什么。有很多方法可以做到这一点,这取决于你想做什么


您可以在函数的最后一行抛出一个错误。只有当所有其他条件子句都失败并且没有执行任何return语句时,才会出现这种情况

throw new Error("Only values below 100,000 are supported")

您可以退回到仅将数字打印为最后一行:

return n.toString()


或者,您可以将返回类型更改为
string | undefined
,只需在末尾添加一个
返回(无值)。这告诉typescript函数可能返回
未定义的
,此函数的调用方需要处理这种情况。

此函数中的每个
返回
语句都嵌套在条件分支中。这意味着,该函数可能会使所有这些条件检查失败,并且不会执行任何return语句。这将导致函数返回
undefined
,这是从未显式返回值的函数的返回值。如果传入的值大于
100000
,则函数将发生这种情况,因为没有条件分支来处理该值

但是,函数的返回类型为
string
,这意味着
undefined
不是允许的返回值。这就是错误的含义

您需要定义当输入超出函数处理的边界时会发生什么。有很多方法可以做到这一点,这取决于你想做什么


您可以在函数的最后一行抛出一个错误。只有当所有其他条件子句都失败并且没有执行任何return语句时,才会出现这种情况

throw new Error("Only values below 100,000 are supported")

您可以退回到仅将数字打印为最后一行:

return n.toString()


或者,您可以将返回类型更改为
string | undefined
,只需在末尾添加一个
返回(无值)。这会告诉typescript,函数可能返回
undefined
,并且该函数的调用方需要处理这种情况。

所有
return
语句都是有条件的。最后需要一个
return
语句。如果代码从未遇到任何条件,则返回一个值。您说过它将返回一个字符串,但有些输入值没有返回。编译器告诉了你们正确的事情。谢谢你们!更清楚了!所有
return
语句都是有条件的。最后需要一个
return
语句。如果代码从未遇到任何条件,则返回一个值。您说过它将返回一个字符串,但有些输入值没有返回。编译器告诉了你们正确的事情。谢谢你们!更清楚了!非常感谢你!你解释得很好,很简单,再次感谢你!既然我已经来了,如果可能的话,我会问另一个问题。。。有时我不想返回空格,我怎么能不返回任何内容而不使用“”?要做到这一点,您可以在我的答案中使用我的第三个可能选项。再见,非常感谢!你解释得很好,很简单,再次感谢你!既然我已经来了,我再问另一个问题