类型系统按照“是否允许隐式类型转换”来分类,可以分为强类型和弱类型。
强类型strongly typed:偏向于不容忍隐式类型转换,比如Pyhon中无法完成整数与字符的相加运算:
>>> 1+"2"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
弱类型weakly typed:允许变量类型的隐式转换,比如C语言中可以允许char类型变量与int类型变量进行相加运算
int a = 1;
char b = '2';
char c = a + b;
printf("c=%d %c\n", c, c);//输出为c=51 3
TypeScript为弱类型,即运行运行时的隐式转换,比如:
console.log(1 + '2');//输出为字符串'12'