ð€¯ åºæïŒã³ãŒãã¯åããã§ãã©ããäžå®âŠ
ããªãã¯ãããŸã§ãJavaScriptã§é«å質ãªã³ãŒããæžãããŠããããã¹ãã§ãã°ãé²ãã¹ãã«ã身ã«ã€ããŸãããããããJavaScriptã«ã¯äžã€ãæ¬è³ªçãªåŒ±ç¹ããããŸããããã¯ãåããææ§ã§ããããšã§ãã
JavaScriptã¯åçåä»ãèšèªã§ããããã¯ã倿°ã®åïŒæååãæ°å€ãªã©ïŒãããã°ã©ã å®è¡äžã«æ±ºãŸããããã³ãŒããæžããŠããæç¹ã§ã¯ã颿°ã«ãæ°åãæž¡ãã€ãããã誀ã£ãŠæååãæž¡ããŠããŸã£ãããšãã£ããã¹ã«æ°ã¥ããªãããšãæå³ããŸãã
ãã®ãã¹ã¯ãããã°ã©ã ãå®è¡ããŠããã®è¡ã«å°éãããŸã§æœåšçãªãã°ãšããŠæœäŒãç¶ããŸãã
ãã®åŒ±ç¹ãæ ¹æ¬ãã解決ãããå®è¡ããåã«ãã»ãŒãã¹ãŠã®åã®ãã¹ãçºèŠãããããã«çãŸããã®ããTypeScript (TS) ã§ããTSã¯ãçŸä»£ã®Webéçºã«ãããŠãå€§èŠæš¡ãããžã§ã¯ãã®äºå®äžã®æšæºãšãªã£ãŠããŸãã
1. âïž TypeScriptãšã¯ïŒãåããå°å ¥ããJavaScript
TypeScriptã¯ãäžèšã§èšãã°ãåãæã€JavaScriptãã§ãã
- TypeScriptïŒã³ãŒããæžãæ®µéïŒã³ã³ãã€ã«åïŒã§åã®ãã§ãã¯ãè¡ãéçåä»ãã
- JavaScriptïŒã³ãŒããå®è¡ãããæ®µéã§åã®ãã§ãã¯ãè¡ãåçåä»ãã
TSã®ä»çµã¿
TypeScriptã®ãã¡ã€ã«ïŒ.tsïŒã¯ããã©ãŠã¶ã§ã¯ãã®ãŸãŸå®è¡ã§ããŸãããéçºäžã«æžãããTSã³ãŒãã¯ãæçµçã«**ãã©ã³ã¹ãã€ã«ïŒå€æïŒ**ãããŠãéåžžã®JavaScriptãã¡ã€ã«ïŒ.jsïŒãšããŠåºåãããŸãã
ãã®å€æãããåã®æ®µéã§ãTSã¯ããªãã®ã³ãŒããé ã ãŸã§ãã§ãã¯ããåã®ãã¹ãããã°ãã®å Žã§ããã«ãšã©ãŒãšããŠæããŠãããã®ã§ãã
2. ð TypeScriptã®åºæ¬ïŒåã®æå®æ¹æ³
TypeScriptã®åŠç¿ã§æãéèŠãªã®ã¯ã倿°ã颿°ã®åŒæ°ãæ»ãå€ã«ãæåŸ ããåããæç€ºçã«æå®ããããšã§ãã
ð 倿°ãžã®å泚é (Type Annotation)
倿°åã®çŽåŸã« :ïŒã³ãã³ïŒãä»ããååãæå®ããŸãã
TypeScript
let username: string = "Taro"; // æååå
let age: number = 25; // æ°å€å
let isActive: boolean = true; // çåœå€å
// â ãšã©ãŒïŒstringåã®å€æ°ã«æ°å€ã代å
¥ããããšããŠãã
// username = 123;
ð 颿°ãžã®å泚é
åŒæ°ãšæ»ãå€ã«ãåãæå®ããŸãã
TypeScript
// åŒæ°priceã¯æ°å€åãæ»ãå€ãæ°å€åã§ããããšãæå®
function calculateTax(price: number): number {
// ããããã§ price.toUpperCase() ãªã©ãšæžããšãTSãå³åº§ã«ãšã©ãŒãåºã
return Math.floor(price * 1.10);
}
// â ãšã©ãŒïŒæåŸ
ãããŠããæ°å€åã§ã¯ãªããæåååãæž¡ããŠãã
// calculateTax("1000");
3. ð¡ïž åå®å šæ§ã®æ©æµïŒãã°ãæªç¶ã«é²ã
TypeScriptãå°å ¥ããæå€§ã®ã¡ãªããã¯ããšãã£ã¿ãŒïŒVS Codeãªã©ïŒã§ã®éçºäœéšãåçã«åäžãããã°ã®çºççãå€§å¹ ã«äœäžããããšã§ãã
3-1. ãªããžã§ã¯ãã®æ§é åïŒã€ã³ã¿ãŒãã§ãŒã¹ïŒ
è€éãªãªããžã§ã¯ãã®ãèšèšå³ããã€ã³ã¿ãŒãã§ãŒã¹ (interface) ãšããŠå®çŸ©ã§ããŸããããã«ããããªããžã§ã¯ãã®ããããã£åãåã®ééãã鲿¢ããŸãã
TypeScript
// ãŠãŒã¶ãŒãªããžã§ã¯ãã®ã圢ããå®çŸ©
interface User {
id: number;
name: string;
email?: string; // ? ã¯çç¥å¯èœïŒãªãã·ã§ãã«ïŒãæå³ãã
}
const alice: User = { id: 1, name: "Alice" };
// â ãšã©ãŒïŒidãnumberåãªã®ã«stringåãæž¡ãããšããŠãã
// const bob: User = { id: "2", name: "Bob" };
3-2. äºæž¬å€æã®åŒ·åïŒã€ã³ããªã»ã³ã¹ïŒ
åæ å ±ãããããšã§ããšãã£ã¿ãŒã次ã«äœ¿ããããããã£ãã¡ãœãããæ£ç¢ºã«äºæž¬ãã衚瀺ããŠãããŸãïŒã€ã³ããªã»ã³ã¹ïŒãããã«ãããã¿ã€ããã¹ãèšæ¶éãã«ãããã°ãæ¿æžããŸãã
ð ãŸãšãïŒTypeScriptã¯æªæ¥ãžã®æè³
TypeScriptã®å°å ¥ã¯ãäžæçã«åŠç¿ã³ã¹ããããããŸããããã®ãªã¿ãŒã³ã¯èšãç¥ããŸããããã°ãå®è¡åã«çºèŠãããããšã§ããããã°ã«ãããæéãå§åçã«æžããå€§èŠæš¡ãªãããžã§ã¯ãã§ãã³ãŒãã®å質ãšå¯èªæ§ãä¿ãããŸãã
- TypeScript: åãæã€JavaScriptãå®è¡åã«åã®ãã¹ããã§ãã¯ããã
- ãã©ã³ã¹ãã€ã«:
.tsãã¡ã€ã«ããã©ãŠã¶ãèªãã.jsãã¡ã€ã«ã«å€æããå·¥çšã - åæ³šé: 倿°ã颿°ã«
:ã䜿ã£ãŠåãæç€ºçã«æå®ããã - ã€ã³ã¿ãŒãã§ãŒã¹: ãªããžã§ã¯ãã®æ§é ãå®çŸ©ããåå®å šæ§ãé«ããã
仿¥ããããªãã®ãããžã§ã¯ãã«TypeScriptãå°å ¥ãã**ãå®è¡ããã°åãã®ãåœããåã**ãšããããã仿§ã®æ¥µããŠå®å®ããéçºç°å¢ãæã«å ¥ããŸãããïŒ

