TS中函数写法的运用

来源:2-7 基础类型快速入门(中)

飞翔的旋律

2023-01-26

我想问下 TS 有两种函数写法,一般比较推荐那种还是根据实际情况在做决定? 请看如下自己写的一个例子:

//函数定义的两种写法,函数中的功能请忽略
const demoExammple = (
    inputValue: string | number
): void => {
    if (!inputValue) {
       console.log({message: 'inputValue is not data or number is 0'});
       return;
    }

    if (vaildIsString(inputValue)) {
        console.log(`the inputvalue lower case = ${inputValue.toLowerCase()}`); 
    } else if (typeof inputValue === 'number') {
        console.log(`the input value is number = ${inputValue}`);
    }
}

function demoExample_fun(inputValue: string | number) : void {
    if (!inputValue) {
        console.log({message: 'inputValue is not data or number is 0'});
        return;
     }
 
     if (vaildIsString(inputValue)) {
         console.log(`the inputvalue lower case = ${inputValue.toLowerCase()}`); 
     } else if (typeof inputValue === 'number') {
         console.log(`the input value is number = ${inputValue}`);
     }
}
写回答

1回答

Dell

2023-02-12

如果单纯就是个函数,建议后者语法

0
0

专为小白设计的TypeScript入门课

Dell老师专为TypeScript小白打造的,全栈式教学TS入门课程

2249 学习 · 506 问题

查看课程