assert functions 続き
昨日書いた assert functions について、リプライで教えてもらったのでメモ。教えてくれた方ありがとうございます。
アロー関数の場合はconst assertIsString: (val: unknown) => asserts val is string = ... とすれば動くかなと. この制約はTSの制御フロー解析の実装方法からきているようですね👀https://t.co/Rf5uJJ84g9 https://t.co/T5U35UHrCe
— Haru. (@utsushiiro) August 9, 2022
なんでそうなるかって話の明確な答えはわかってないですが、この記事とその記事内の TypeScript の GitHub の Issue を見るとちょっと理解がはかどるかもしれませんhttps://t.co/wCICOFh5YX
— nabeliwo / なべりを (@nabeliwo) August 10, 2022
↑この記事に自分が躓いていたことがそのまんま書いてあったので助かりました。
どうしてこのようなエラーが発生するのかは分かりませんが、下記のissueによれば、この仕様は意図的なものだそうです。
Generic type uplifting during assignment (ts 2775) · Issue #41047 · microsoft/TypeScript
ふむふむ。