Archives / 2021 / August
  • A Stupid Little TypeScript Mistake (Part 2)

    After my blog post in June, here is another tale from a C# developer getting his feet wet with TypeScript.

    TypeScript’s type checking makes my life so much easier, but it does not catch everything. Recently I forgot an important part of a for-loop. Here is a stripped-down example:

    var items=["item0", "item1", "item2"];
    for (var n=0;items.length;n++)
        // ...

    The code compiles, but runs into an endless loop – the “n<” is missing in the condition.

    If I write similar code in C#…

    var items=new[] {"item0", "item1", "item2"};
    for (var n=0;items.Length;n++)
        // ...

    … the compiler tells me what I did wrong:

    Cannot implicitly convert type 'int' to 'bool'

    Ok, another (TypeScript) lesson learned.