TypeScript ---- 类型注解之函数
// 参数 返回值
//Ts只会检查类型 不会检查里面的逻辑
// 参数要加类型注解 返回值会类型推断 但是建议添加
const addNums = (a :number, b : number):number {
return a - b;
}
function mutiply(a : number, b : number):number{
return a * b;
}
const divide = function(a:number, b:number):number{
return a / b;
}
const logger = (message : string): void=>{
console.log(message);
// return undefined;
// return null;
}
const throwError =(message :string) :never =>{
throw new Error(message);
}
const throwError1 =(message :string) :string =>{
if(!message){
throw new Error(message);
}
return message;
}
//未加解构 添加类型注解
const todayWeather ={
data:new Date(),
weather:'晴天',
}
const logWeather = (todayWeather:{data : Date, weather: string}):void=>{
console.log(todayWeather.data);
console.log(todayWeather.weather);
}
logWeather(todayWeather);
//解构 添加类型注解
const todayWeather ={
data:new Date(),
weather:'晴天',
}
const logWeather = ({data, weather}:{data : Date, weather: string}):void =>{
console.log(data);
console.log(weather);
}
logWeather(todayWeather);