Typescript只能捕获编译时类型错误,不能捕获运行时类型错误。这意味着编写的代码即使通过了类型检查,在运行的时候仍然可能出现错误。

    1. function messUpTheArray(arr: Array<string | number>): void {
    2. arr.push(3);
    3. }
    4. const strings: Array<string> = ['foo', 'bar'];
    5. messUpTheArray(strings);
    6. const s: string = strings[2];
    7. console.log(s.toLowerCase()) // Uncaught TypeError: s.toLowerCase is not a function