â» ç½®ãæããããªãã±ã¼ã¹ããããã¨æãã¾ãã®ã§ããæ¿ç¥ãããã ããã
JavaScriptã®å³æé¢æ°ã使ã£ã¦ãletã使ãããã«ãªãã¨ãããconstã§å®ç¾©ã§ããããã«ãªããã¨ãããã å ·ä½çã«ã¯æ¡ä»¶åå²ã§æ´»èºããã è¤éãªifæãswitchæã§å¤æ°ã®å¤ãå¤ããã¨ãã«ä½¿ããã ãµã³ãã«ã³ã¼ããæ¸ãã¦ããã
let str: string = ""; if (cond1) { str = "hoge"; } else if (cond2) { str = "fuga"; } else if (cond3) { str = "piyo"; } else { str = "hogehoge"; }
JavaScriptã¨è¨ãã¤ã¤ãTypeScriptãcond1, cond2, cond3ã«ã¯æååæ¯è¼ãæ°å¤æ¯è¼ãªã©ã®å¼ãå ¥ãããã®ã³ã¼ããå³æé¢æ°ã使ã£ã¦æ¸ãç´ãã¨ä»¥ä¸ã®ããã«ãªãã
const str = (() => { if (cond1) { return "hoge"; } if (cond2) { return "fuga"; } if (cond3) { return "piyo"; } return "hogehoge"; })();
åãã¦è¦ãæã¯é¢é£ãã£ãããæ £ããã¨èªã¿ãããæããã
å³æé¢æ°ãããªãã¦ã¡ããã¨é¢æ°ã¨ãã¦å®ç¾©ããã°ãããããã¨ããæè¦ã¯ããã£ã¨ãã§ãã