const add = function(x: number, y: number): number {
return x + y;
};
console.log(add('hello', 5));
本意是只接受输入数字,可是传递字符串还是可以编译并执行最终的js代码,哪里出问题了?
const add = function(x: number, y: number): number {
return x + y;
};
console.log(add('hello', 5));
本意是只接受输入数字,可是传递字符串还是可以编译并执行最终的js代码,哪里出问题了?
这是TypeScript? 如果有类型错误时,使用
tsc
编译时会报错:我用VsCode写的也会提示错误:
但是还是会把
ts
文件编译成js
文件,TypeScript只是帮你找出安全隐患。TS官网的原话:
Notice that although there were errors, the greeter.js file is still created. You can use TypeScript even if there are errors in your code. But in this case, TypeScript is warning that your code will likely not run as expected.