≪ Today I learned.
RSS購読
    公開日
    タグ
    TypeScript
    著者
    ダーシノ

    TypeScript 5.6 Betaがリリースされた

    先日、TypeScript 5.6 Betaがリリースされた。

    Disallowed Nullish and Truthy Checkes

    文法的には正しいけど開発者の意図と異なるような条件(常にtrue/nullishになるなど)をチェックしてくれるようになった。

    意図しないTruthyチェック

    // Before: No error
    if (/0x[0-9a-f]/) {
      // 条件式はTruthyになるが開発者の意図とは異なる
      // 本来なら /0x[0-9a-f]/.test(val) とすべき
    }
    
    
    // After
    if (/0x[0-9a-f]/) {
    //  ^^^^^^^^^^^^
    //  error: This kind of expression is always truthy.
    }

    意図しないNullishチェック

    // Before: No error
    function isValid(val: string | number, options: any) {
      // val < options.mapの結果はbooleanでNullish valueにはならないため、100が絶対に返らない
      return val < options.max ?? 100
    }
    
    // After
    function isValid(val: string | number, options: any) {
      return val < options.max ?? 100
    //       ^^^^^^^^^^^^^^^^^
    // error: Right operand of ?? is unreachable because the left operand is never nullish.
    }

    Iterator Helper Methods

    Generatorが返すオブジェクトにIterator#map()Iterator#take()メソッドが追加された。

    ECMAScriptのIterator HelpersがStage3になったことに伴い、TypeScript 5.6からもサポートされるようになった。

    function* positiveIntegers() {
        let i = 1;
        while (true) {
            yield i;
            i++;
        }
    }
    
    const evenNumbers = positiveIntegers().map(x => x * 2);
    
    // Output:
    //    2
    //    4
    //    6
    //    8
    //   10
    for (const value of evenNumbers.take(5)) {
        console.log(value);
    }