🌼 はじめに 皆さんは Javascript である値を boolean に変換するときどういう方法も使いますか?よく使われる方法は!!(二重否定・Double negation)か、Boolean()だと思います。 const hello = Boolean("hello"); // true const world = !!"world" // true Typescript のハンドブックでもその2つを紹介してます。 You can always coerce values to booleans by running them through the Boolean function, or by using the shorter double-Boolean negation. いちおう型の観点では、!!を使ったら型がtrueかfalseになり、Boolean()関数を使った
![Typescript では !! と Boolean() が完全に同じ動作ではない](https://cdn-ak-scissors.b.st-hatena.com/image/square/e27c7e22d782ced823454e81f70b68edbef6add9/height=288;version=1;width=512/https%3A%2F%2Fres.cloudinary.com%2Fzenn%2Fimage%2Fupload%2Fs--F8U67bz---%2Fc_fit%252Cg_north_west%252Cl_text%3Anotosansjp-medium.otf_55%3ATypescript%252520%2525E3%252581%2525A7%2525E3%252581%2525AF%252520%252521%252521%252520%2525E3%252581%2525A8%252520Boolean%252528%252529%252520%2525E3%252581%25258C%2525E5%2525AE%25258C%2525E5%252585%2525A8%2525E3%252581%2525AB%2525E5%252590%25258C%2525E3%252581%252598%2525E5%25258B%252595%2525E4%2525BD%25259C%2525E3%252581%2525A7%2525E3%252581%2525AF%2525E3%252581%2525AA%2525E3%252581%252584%252Cw_1010%252Cx_90%252Cy_100%2Fg_south_west%252Cl_text%3Anotosansjp-medium.otf_37%3A%2525E3%252581%2525BF%2525E3%252582%252593%2525E3%252581%2525A1%2525E3%252582%252583%2525E3%252582%252593%252Cx_203%252Cy_121%2Fg_south_west%252Ch_90%252Cl_fetch%3AaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL3plbm4tdXNlci11cGxvYWQvYXZhdGFyLzA3YTQwNzFlZWIuanBlZw%3D%3D%252Cr_max%252Cw_90%252Cx_87%252Cy_95%2Fv1627283836%2Fdefault%2Fog-base-w1200-v2.png)