JavaScriptと同様に「'abc'.match(/b/)[0]」みたいな書き方をするとTypeScriptではコンパイルエラーになってしまいます。
回避する方法が以下。
回避する方法が以下。
JavaScriptなら問題なく実行できます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('abc'.match(/b/)[0]) |
同じ内容でもTypeScriptだと「オブジェクトはおそらく「null」です」というコンパイルエラーになってしまいます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('abc'.match(/b/)[0]) |
「typescript regexp null」で検索して以下のページに辿り着きました。
javascript - Object is possibly 'null' for regex - Stack Overflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('abc'.match(/b/)![0]) |
とりあえず使えるようにはなりましたが、全然意味が分かりません…。
いずれ時間ができたら、どういう理屈なのか調べたいです。
0 件のコメント:
コメントを投稿