// コンパイルエラー const numbers: NonEmptyArray<number> = []; // これはコンパイルが通る const numbers: NonEmptyArray<number> = [1]; const needNonEmpty = (arr: NonEmptyArray<number>) => arr.length const a: number[] = [1,2,3]; const b: NonEmptyArray<number> = [1,2,3]; const isNonEmptyArray = <T>(arr: T[]): arr is NonEmptyArray<T> => arr.length > 0; // コンパイルエラー needNonEmpty(a); // これはコンパイルが通る needNonEmpty(b); // これはコン
![TypeScriptで空でない配列を型で表現する](https://cdn-ak-scissors.b.st-hatena.com/image/square/40f656e07a0ab43d9179733f07bf87af853d2440/height=288;version=1;width=512/https%3A%2F%2Fres.cloudinary.com%2Fzenn%2Fimage%2Fupload%2Fs--TOjujTCP--%2Fc_fit%252Cg_north_west%252Cl_text%3Anotosansjp-medium.otf_55%3ATypeScript%2525E3%252581%2525A7%2525E7%2525A9%2525BA%2525E3%252581%2525A7%2525E3%252581%2525AA%2525E3%252581%252584%2525E9%252585%25258D%2525E5%252588%252597%2525E3%252582%252592%2525E5%25259E%25258B%2525E3%252581%2525A7%2525E8%2525A1%2525A8%2525E7%25258F%2525BE%2525E3%252581%252599%2525E3%252582%25258B%252Cw_1010%252Cx_90%252Cy_100%2Fg_south_west%252Cl_text%3Anotosansjp-medium.otf_37%3Amm1995tk%252Cx_203%252Cy_121%2Fg_south_west%252Ch_90%252Cl_fetch%3AaHR0cHM6Ly9zdG9yYWdlLmdvb2dsZWFwaXMuY29tL3plbm4tdXNlci11cGxvYWQvYXZhdGFyLzUyMmFiODcxNjUuanBlZw%3D%3D%252Cr_max%252Cw_90%252Cx_87%252Cy_95%2Fv1627283836%2Fdefault%2Fog-base-w1200-v2.png)