string; } const hoge: Hoge[] = [ { // Type 'number' is not assignable to type 'string'. fuga: 1 } ]; // Type 'number' is not assignable to type 'string'. const piyo : string[] = [1];
{ if (typeof x === 'string') { // OK x.substring(1); } else { // Property 'substring' does not exist on type 'number'. x.substring(1) } // Property 'substring' does not exist on type 'string | number'. x.substring(1); } 型ガードができる