{"id":85275,"date":"2025-03-26T15:11:36","date_gmt":"2025-03-26T08:11:36","guid":{"rendered":"https:\/\/itviecblog.uptech.vn\/?p=85275"},"modified":"2025-06-17T15:27:08","modified_gmt":"2025-06-17T08:27:08","slug":"cau-hoi-phong-van-typescript-2","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/","title":{"rendered":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#Tong_quan_ve_TypeScript\" >T\u1ed5ng quan v\u1ec1 TypeScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#Cau_hoi_phong_van_TypeScript_cho_Fresher\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript cho Fresher<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#Cau_hoi_phong_van_TypeScript_cho_Junior_hoac_Senior_Developer\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript cho Junior ho\u1eb7c Senior Developer\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#Cau_hoi_phong_van_TypeScript_ve_type_inference\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript v\u1ec1 type inference\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#Cau_hoi_phong_van_TypeScript_xu_ly_tinh_huong\" >C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#Tong_ket_ve_cau_hoi_phong_van_TypeScript\" >T\u1ed5ng k\u1ebft v\u1ec1 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript\u00a0<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>\u0110\u1ec3 s\u1ef1 chu\u1ea9n b\u1ecb t\u1ed1t cho bu\u1ed5i ph\u1ecfng v\u1ea5n, b\u1ea1n c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c v\u1ec1 TypeScript t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao. B\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript sau \u0111\u00e2y bao g\u1ed3m c\u00e1c c\u00e2u h\u1ecfi v\u1ec1 c\u00fa ph\u00e1p, th\u1ef1c h\u00e0nh m\u00e3, gi\u1ea3i \u0111\u00e1p v\u1ec1 type safety hay c\u00e1ch x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng khi d\u00f9ng TypeScript trong l\u1eadp tr\u00ecnh s\u1ebd gi\u00fap b\u1ea1n c\u00f3 g\u00f3c nh\u00ecn to\u00e0n di\u1ec7n h\u01a1n v\u1ec1 TypeScript.<\/strong><\/em><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ecdc b\u00e0i vi\u1ebft sau \u0111\u00e2y \u0111\u1ec3 \u0111\u01b0\u1ee3c gi\u1ea3i \u0111\u00e1p chi ti\u1ebft h\u01a1n v\u1ec1:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript d\u00e0nh cho Fresher<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u00f9 h\u1ee3p v\u1edbi Junior ho\u1eb7c Senior Developer<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi\u00a0ph\u1ecfng v\u1ea5n TypeScript v\u1ec1 type safety<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript v\u1ec1 type inference<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Tong_quan_ve_TypeScript\"><\/span><b>T\u1ed5ng quan v\u1ec1 TypeScript<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">TypeScript l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef ngu\u1ed3n m\u1edf \u0111\u01b0\u1ee3c ph\u00e1t tri\u1ec3n b\u1edfi Anders Hejlsberg t\u1ea1i Microsoft. \u0110\u00e2y l\u00e0 m\u1ed9t si\u00eau t\u1eadp JavaScript \u0111\u01b0\u1ee3c g\u00f5 t\u0129nh, bi\u00ean d\u1ecbch th\u00e0nh JavaScript thu\u1ea7n t\u00fay. M\u00e3 TypeScript sau khi bi\u00ean d\u1ecbch c\u00f3 th\u1ec3 ch\u1ea1y tr\u00ean m\u1ecdi tr\u00ecnh duy\u1ec7t, m\u00e1y ch\u1ee7 v\u00e0 h\u1ec7 \u0111i\u1ec1u h\u00e0nh h\u1ed7 tr\u1ee3 JavaScript.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u \u0111\u00f3 c\u0169ng c\u00f3 ngh\u0129a l\u00e0 t\u1ea5t c\u1ea3 m\u00e3 JavaScript \u0111\u1ec1u h\u1ee3p l\u1ec7 cho TypeScript, gi\u00fap cung c\u1ea5p c\u00e1c t\u00ednh n\u0103ng n\u00e2ng cao nh\u01b0 IntelliSense, ho\u00e0n th\u00e0nh m\u00e3 ho\u1eb7c t\u00e1i c\u1ea5u tr\u00fac m\u00e3 an to\u00e0n,&#8230;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">TypeScript b\u1ed5 sung c\u00e1c t\u00ednh n\u0103ng ng\u00f4n ng\u1eef v\u00e0 ki\u1ec3u t\u0129nh t\u00f9y ch\u1ecdn nh\u01b0 l\u1edbp v\u00e0 m\u00f4-\u0111un. \u0110i\u1ec1u quan tr\u1ecdng c\u1ea7n bi\u1ebft l\u00e0 t\u1ea5t c\u1ea3 c\u00e1c t\u00ednh n\u0103ng n\u00e2ng cao n\u00e0y kh\u00f4ng t\u1ed1n chi ph\u00ed cho JavaScript. Sau khi bi\u00ean d\u1ecbch, k\u1ebft qu\u1ea3 s\u1ebd l\u00e0 m\u00e3 JavaScript thu\u1ea7n t\u00fay. TypeScript l\u00e0 ng\u00f4n ng\u1eef ph\u00f9 h\u1ee3p \u0111\u1ec3 ph\u00e1t tri\u1ec3n JavaScript \u1edf c\u00e1c d\u1ef1 \u00e1n ph\u00e1t tri\u1ec3n \u1ee9ng d\u1ee5ng (application).<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>\u0110i\u1ec3m m\u1ea1nh<\/b><\/td>\n<td><b>\u0110i\u1ec3m y\u1ebfu<\/b><\/td>\n<\/tr>\n<tr>\n<td>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">M\u00e3 an to\u00e0n v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00f3 h\u1ed7 tr\u1ee3 ch\u1ee9c n\u0103ng IntelliSense v\u00e0 IDE.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">H\u1ed7 tr\u1ee3 tr\u00ean nhi\u1ec1u tr\u00ecnh duy\u1ec7t kh\u00e1c nhau.<\/span><\/li>\n<\/ul>\n<\/td>\n<td>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Kh\u00f4ng ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c d\u1ef1 \u00e1n quy m\u00f4 nh\u1ecf.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u1ea7n th\u00eam c\u00e1c b\u01b0\u1edbc x\u00e2y d\u1ef1ng m\u00e3<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">C\u00e1c c\u00fa ph\u00e1p ho\u1eb7c ki\u1ec3u b\u1ed5 sung c\u00f3 th\u1ec3 khi\u1ebfn m\u00e3 ph\u1ee9c t\u1ea1p h\u01a1n.<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <a href=\"\/blog\/typescript-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>TypeScript l\u00e0 g\u00ec: T\u00ecm hi\u1ec3u chi ti\u1ebft v\u1ec1 c\u00e1ch ho\u1ea1t \u0111\u1ed9ng c\u1ee7a TypeScript<\/strong><\/a><\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_TypeScript_cho_Fresher\"><\/span><b> C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript cho Fresher<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>C\u00e1c ki\u1ec3u d\u1eef li\u1ec7u nguy\u00ean th\u1ee7y trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">TypeScript c\u00f3 c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u ph\u1ed5 bi\u1ebfn th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng l\u00e0 string, number v\u00e0 boolean. Ngo\u00e0i ra c\u00f2n nhi\u1ec1u ki\u1ec3u d\u1eef li\u1ec7u kh\u00e1c. Ch\u00fang t\u01b0\u01a1ng \u1ee9ng v\u1edbi c\u00e1c ki\u1ec3u d\u1eef li\u1ec7u trong JavaScript. Trong \u0111\u00f3:\u00a0<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>string:<\/b><span style=\"font-weight: 400;\"> Bi\u1ec3u di\u1ec5n c\u00e1c gi\u00e1 tr\u1ecb v\u0103n b\u1ea3n nh\u01b0 \u201cjavascript\u201d, \u201ctypescript\u201d,&#8230;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>number:<\/b><span style=\"font-weight: 400;\"> Bi\u1ec3u di\u1ec5n c\u00e1c gi\u00e1 tr\u1ecb s\u1ed1 nh\u01b0 1, 2, 32, 43,..<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>boolean:<\/b><span style=\"font-weight: 400;\"> Bi\u1ec3u di\u1ec5n m\u1ed9t bi\u1ebfn c\u00f3 th\u1ec3 c\u00f3 gi\u00e1 tr\u1ecb \u2018true\u2019 ho\u1eb7c \u2018false\u2019<\/span><\/li>\n<\/ul>\n<h3><b>Gi\u1ea3i th\u00edch c\u00e1ch m\u1ea3ng (array) ho\u1ea1t \u0111\u1ed9ng trong TypeScript<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng array \u0111\u1ec3 l\u01b0u tr\u1eef c\u00e1c gi\u00e1 tr\u1ecb c\u00f9ng lo\u1ea1i. M\u1ea3ng l\u00e0 t\u1eadp h\u1ee3p c\u00e1c gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c s\u1eafp x\u1ebfp v\u00e0 l\u1eadp ch\u1ec9 m\u1ee5c. Vi\u1ec7c l\u1eadp ch\u1ec9 m\u1ee5c (index) b\u1eaft \u0111\u1ea7u t\u1eeb 0, t\u1ee9c l\u00e0 ph\u1ea7n t\u1eed \u0111\u1ea7u ti\u00ean c\u00f3 ch\u1ec9 m\u1ee5c 0, ph\u1ea7n t\u1eed th\u1ee9 hai c\u00f3 ch\u1ec9 m\u1ee5c 1.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let values: number[] = [];<\/span>\n<span style=\"font-weight: 400;\">values[0] = 10;<\/span>\n<span style=\"font-weight: 400;\">values[1] = 20;<\/span>\n<span style=\"font-weight: 400;\">values[2] = 30;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t m\u1ea3ng b\u1eb1ng c\u00fa ph\u00e1p vi\u1ebft t\u1eaft nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let values: number[] = [15, 20, 25, 30];<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, TypeScript cung c\u1ea5p c\u00fa ph\u00e1p thay th\u1ebf \u0111\u1ec3 ch\u1ec9 \u0111\u1ecbnh ki\u1ec3u m\u1ea3ng (array):<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let values: Array&lt;number&gt; = [15, 20, 25, 30];<\/span><\/pre>\n<h3><b>Void l\u00e0 g\u00ec, v\u00e0 khi n\u00e0o s\u1eed d\u1ee5ng ki\u1ec3u void?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong TypeScript, \u201cvoid\u201d l\u00e0 ki\u1ec3u d\u1eef li\u1ec7u d\u00f9ng \u0111\u1ec3 ch\u1ec9 ra r\u1eb1ng m\u1ed9t h\u00e0m kh\u00f4ng tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb n\u00e0o. N\u00f3 th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng cho c\u00e1c h\u00e0m ch\u1ec9 th\u1ef1c hi\u1ec7n t\u00e1c v\u1ee5 m\u00e0 kh\u00f4ng c\u1ea7n tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function notify(): void {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0alert(\"The user has been notified.\");<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Kh\u00f4ng gi\u1ed1ng any (ch\u1ea5p nh\u1eadn m\u1ecdi ki\u1ec3u), void bi\u1ec3u th\u1ecb s\u1ef1 v\u1eafng m\u1eb7t c\u1ee7a gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1. L\u01b0u \u00fd, bi\u1ebfn ki\u1ec3u void \u00edt \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng tr\u1ef1c ti\u1ebfp, v\u00e0 theo c\u1ea5u h\u00ecnh strictNullChecks, b\u1ea1n ch\u1ec9 c\u00f3 th\u1ec3 g\u00e1n undefined cho n\u00f3 tr\u1eeb khi khai b\u00e1o r\u00f5 r\u00e0ng ki\u1ec3u union nh\u01b0 void | null.&#8221;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 th\u00eam v\u00ed d\u1ee5 v\u1ec1 h\u00e0m tr\u1ea3 v\u1ec1 undefined so s\u00e1nh v\u1edbi void \u0111\u1ec3 l\u00e0m r\u00f5.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function returnsUndefined(): undefined {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0return undefined; \/\/ Ph\u1ea3i tr\u1ea3 v\u1ec1 r\u00f5 r\u00e0ng<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>Gi\u1ea3i th\u00edch c\u00fa ph\u00e1p h\u00e0m m\u0169i t\u00ean (arrow function) trong TypeScript<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">H\u00e0m m\u0169i t\u00ean (Arrow function) trong TypeScript c\u00f3 c\u00fa ph\u00e1p ng\u1eafn g\u1ecdn h\u01a1n h\u00e0m th\u00f4ng th\u01b0\u1eddng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 hi\u1ec3u h\u01a1n v\u1ec1 arrow function, b\u1ea1n c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n m\u1ed9t v\u00ed d\u1ee5 nh\u1ecf v\u1ec1 m\u1ed9t h\u00e0m th\u00f4ng th\u01b0\u1eddng c\u1ed9ng hai s\u1ed1 v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t s\u1ed1.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function add(x: number, y: number): number {<\/span>\n<span style=\"font-weight: 400;\">let sum = x + y;<\/span>\n<span style=\"font-weight: 400;\">return sum;<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Sau \u0111\u00f3, s\u1eed d\u1ee5ng arrow function \u0111\u1ec3 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let add = (x: number, y: number): number =&gt; {<\/span>\n<span style=\"font-weight: 400;\">let sum = x + y;<\/span>\n<span style=\"font-weight: 400;\">return sum;<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 \u0111\u01a1n gi\u1ea3n h\u00f3a c\u00fa ph\u00e1p h\u01a1n n\u1eefa b\u1eb1ng c\u00e1ch lo\u1ea1i b\u1ecf d\u1ea5u ngo\u1eb7c v\u00e0 c\u00e2u l\u1ec7nh return. \u0110i\u1ec1u n\u00e0y cho ph\u00e9p khi th\u00e2n h\u00e0m ch\u1ec9 bao g\u1ed3m m\u1ed9t c\u00e2u l\u1ec7nh. V\u00ed d\u1ee5, n\u1ebfu lo\u1ea1i b\u1ecf bi\u1ebfn t\u1ed5ng t\u1ea1m th\u1eddi, c\u00f3 th\u1ec3 vi\u1ebft l\u1ea1i h\u00e0m tr\u00ean nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let add = (x: number, y: number): number =&gt; x + y;<\/span><\/pre>\n<h3><b>B\u1ea1n hi\u1ec3u g\u00ec v\u1ec1 null v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng ch\u00fang trong TypeScript<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong l\u1eadp tr\u00ecnh, gi\u00e1 tr\u1ecb null bi\u1ec3u th\u1ecb cho gi\u00e1 tr\u1ecb kh\u00f4ng t\u1ed3n t\u1ea1i ho\u1eb7c tr\u1ed1ng. Bi\u1ebfn null kh\u00f4ng tr\u1ecf \u0111\u1ebfn b\u1ea5t k\u1ef3 \u0111\u1ed1i t\u01b0\u1ee3ng n\u00e0o. Do \u0111\u00f3, b\u1ea1n kh\u00f4ng th\u1ec3 truy c\u1eadp b\u1ea5t k\u1ef3 thu\u1ed9c t\u00ednh n\u00e0o tr\u00ean bi\u1ebfn ho\u1eb7c g\u1ecdi ph\u01b0\u01a1ng th\u1ee9c tr\u00ean bi\u1ebfn \u0111\u00f3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong TypeScript, gi\u00e1 tr\u1ecb null \u0111\u01b0\u1ee3c ch\u1ec9 ra b\u1eb1ng t\u1eeb kh\u00f3a \u2018null\u2019. B\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m tra xem gi\u00e1 tr\u1ecb c\u00f3 ph\u1ea3i l\u00e0 null hay kh\u00f4ng nh\u01b0 sau:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function greet(name: string | null) {<\/span>\n<span style=\"font-weight: 400;\">if (name === null) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0console.log(\"Name is not provided\");<\/span>\n<span style=\"font-weight: 400;\">} else {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0console.log(\"Good morning, \" + name.toUpperCase());<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">var foo = null;<\/span>\n<span style=\"font-weight: 400;\">greet(foo); \/\/ \"Name is not provided\"<\/span>\n\n<span style=\"font-weight: 400;\">foo = \"Anders\";<\/span>\n<span style=\"font-weight: 400;\">greet(foo);\u00a0 \/\/ \"Good morning, ANDERS\"<\/span><\/pre>\n<h3><b>Undefined trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">undefined xu\u1ea5t hi\u1ec7n khi m\u1ed9t bi\u1ebfn \u0111\u01b0\u1ee3c khai b\u00e1o nh\u01b0ng ch\u01b0a \u0111\u01b0\u1ee3c g\u00e1n gi\u00e1 tr\u1ecb. undefined kh\u00f4ng \u0111em l\u1ea1i hi\u1ec7u qu\u1ea3 cao n\u1ebfu ch\u1ec9 \u0111\u1ee9ng m\u1ed9t m\u00ecnh.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">M\u1ed9t bi\u1ebfn l\u00e0 undefined n\u1ebfu n\u00f3 \u0111\u01b0\u1ee3c khai b\u00e1o, nh\u01b0ng kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb n\u00e0o \u0111\u01b0\u1ee3c g\u00e1n cho n\u00f3. Ng\u01b0\u1ee3c l\u1ea1i, null \u0111\u01b0\u1ee3c g\u00e1n cho m\u1ed9t bi\u1ebfn v\u00e0 n\u00f3 kh\u00f4ng bi\u1ec3u di\u1ec5n gi\u00e1 tr\u1ecb n\u00e0o.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">console.log(null == null); \/\/ true<\/span>\n<span style=\"font-weight: 400;\">console.log(undefined == undefined); \/\/ true<\/span>\n<span style=\"font-weight: 400;\">console.log(null == undefined); \/\/ true, with type-conversion<\/span>\n<span style=\"font-weight: 400;\">console.log(null === undefined); \/\/ false, without type-conversion<\/span>\n<span style=\"font-weight: 400;\">console.log(0 == undefined); \/\/ false<\/span>\n<span style=\"font-weight: 400;\">console.log('' == undefined); \/\/ false<\/span>\n<span style=\"font-weight: 400;\">console.log(false == undefined); \/\/ false<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">To\u00e1n t\u1eed == cho ph\u00e9p \u00e9p ki\u1ec3u n\u00ean null == undefined tr\u1ea3 v\u1ec1 true, trong khi === ki\u1ec3m tra c\u1ea3 ki\u1ec3u d\u1eef li\u1ec7u n\u00ean tr\u1ea3 v\u1ec1 false.<\/span><\/p>\n<h3><b>To\u00e1n t\u1eed typeof l\u00e0 g\u00ec? S\u1eed d\u1ee5ng nh\u01b0 th\u1ebf n\u00e0o trong TypeScript?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">T\u01b0\u01a1ng t\u1ef1 nh\u01b0 JavaScript, to\u00e1n t\u1eed typeof trong TypeScript tr\u1ea3 v\u1ec1 ki\u1ec3u c\u1ee7a to\u00e1n h\u1ea1ng d\u01b0\u1edbi d\u1ea1ng chu\u1ed7i.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">console.log(typeof 10);\u00a0 \/\/ \"number\"<\/span>\n\n<span style=\"font-weight: 400;\">console.log(typeof 'foo');\u00a0 \/\/ \"string\"<\/span>\n\n<span style=\"font-weight: 400;\">console.log(typeof false);\u00a0 \/\/ \"boolean\"<\/span>\n\n<span style=\"font-weight: 400;\">console.log(typeof bar);\u00a0 \/\/ \"undefined\"<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, trong TypeScript, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng to\u00e1n t\u1eed typeof trong ng\u1eef c\u1ea3nh \u0111\u1ec3 tham chi\u1ebfu \u0111\u1ebfn ki\u1ec3u c\u1ee7a thu\u1ed9c t\u00ednh ho\u1eb7c bi\u1ebfn.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let greeting = \"hello\";<\/span>\n\n<span style=\"font-weight: 400;\">let typeOfGreeting: typeof greeting;\u00a0 \/\/ T\u01b0\u01a1ng \u0111\u01b0\u01a1ng v\u1edbi let typeOfGreeting : string<\/span><\/pre>\n<h3><b>Cung c\u1ea5p c\u00fa ph\u00e1p cho tham s\u1ed1 t\u00f9y ch\u1ecdn trong TypeScript<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">M\u1ed9t h\u00e0m c\u00f3 th\u1ec3 \u0111\u00e1nh d\u1ea5u m\u1ed9t ho\u1eb7c nhi\u1ec1u tham s\u1ed1 c\u1ee7a n\u00f3 l\u00e0 t\u00f9y ch\u1ecdn b\u1eb1ng c\u00e1ch th\u00eam th\u00eam d\u1ea5u ? sau t\u00ean tham s\u1ed1. Trong v\u00ed d\u1ee5 d\u01b0\u1edbi \u0111\u00e2y, tham s\u1ed1 greeting \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u l\u00e0 t\u00f9y ch\u1ecdn.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function greet(name: string, greeting?: string) {<\/span>\n<span style=\"font-weight: 400;\">if (!greeting)<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0greeting = \"Hello\";<\/span>\n\n<span style=\"font-weight: 400;\">console.log(`${greeting}, ${name}`);<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">greet(\"John\", \"Hi\");\u00a0 \/\/ Hi, John<\/span>\n<span style=\"font-weight: 400;\">greet(\"Mary\", \"Hola\");\u00a0 \/\/ Hola, Mary<\/span>\n<span style=\"font-weight: 400;\">greet(\"Jane\");\u00a0 \/\/ Hello, Jane<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y, n\u1ebfu greeting kh\u00f4ng \u0111\u01b0\u1ee3c cung c\u1ea5p, n\u00f3 s\u1ebd l\u00e0 undefined v\u00e0 \u0111\u01b0\u1ee3c g\u00e1n gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh l\u00e0 &#8216;Hello&#8217;.&#8221;*<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00f3 th\u1ec3 th\u00eam l\u01b0u \u00fd v\u1ec1 c\u00e1ch \u0111\u1eb7t gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh tr\u1ef1c ti\u1ebfp b\u1eb1ng greeting: string = &#8216;Hello&#8217; thay v\u00ec d\u00f9ng if, nh\u01b0:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function greet(name: string, greeting: string = 'Hello') {\u00a0 <\/span>\n<span style=\"font-weight: 400;\"> \u00a0console.log(`${greeting}, ${name}`);\u00a0\u00a0<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>M\u1ee5c \u0111\u00edch c\u1ee7a t\u1ec7p tsconfig.json l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">tsconfig.json l\u00e0 t\u1ec7p c\u1ea5u h\u00ecnh cho tr\u00ecnh bi\u00ean d\u1ecbch TypeScript, ch\u1ee9a th\u00f4ng tin nh\u01b0 \u0111\u01b0\u1eddng d\u1eabn t\u1ec7p ngu\u1ed3n (include, exclude) hay c\u00e0i \u0111\u1eb7t bi\u00ean d\u1ecbch (compilerOptions).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">{<\/span>\n<span style=\"font-weight: 400;\"> \"compilerOptions\": {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\"module\": \"system\",<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\"noImplicitAny\": true,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\"removeComments\": true,<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\"outFile\": \"..\/..\/built\/local\/tsc.js\",<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\"sourceMap\": true<\/span>\n<span style=\"font-weight: 400;\"> },<\/span>\n<span style=\"font-weight: 400;\"> \"include\": [\"src\/**\/*\"],<\/span>\n<span style=\"font-weight: 400;\"> \"exclude\": [\"node_modules\", \"**\/*.spec.ts\"]<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 ki\u1ec3m tra null ch\u1eb7t ch\u1ebd trong TypeScript?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">null l\u00e0 m\u1ed9t trong nh\u1eefng ngu\u1ed3n ph\u1ed5 bi\u1ebfn nh\u1ea5t g\u00e2y ra nh\u1eefng l\u1ed7i runtime kh\u00f4ng mong mu\u1ed1n trong l\u1eadp tr\u00ecnh. TypeScript s\u1ebd gi\u00fap tr\u00e1nh ch\u00fang b\u1eb1ng c\u00e1ch th\u1ef1c thi ki\u1ec3m tra null nghi\u00eam ng\u1eb7t.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 th\u1ef1c thi ki\u1ec3m tra null nghi\u00eam ng\u1eb7t theo hai c\u00e1ch:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng t\u00f9y ch\u1ecdn <\/span><span style=\"font-weight: 400;\">&#8211;strictNullChecks<\/span><span style=\"font-weight: 400;\"> cho tr\u00ecnh bi\u00ean d\u1ecbch TypeScript (tsc)<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1eb7t thu\u1ed9c t\u00ednh <\/span><span style=\"font-weight: 400;\">strictNullChecks<\/span><span style=\"font-weight: 400;\"> th\u00e0nh true trong t\u1ec7p c\u1ea5u h\u00ecnh tsconfig.json.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Khi t\u1eaft strictNullChecks TypeScript b\u1ecf qua c\u00e1c gi\u00e1 tr\u1ecb null v\u00e0 undefined trong m\u00e3. Khi l\u00e0 true, null v\u00e0 undefined c\u00f3 c\u00e1c ki\u1ec3u ri\u00eang bi\u1ec7t. Tr\u00ecnh bi\u00ean d\u1ecbch s\u1ebd \u0111\u01b0a ra l\u1ed7i ki\u1ec3u n\u1ebfu b\u1ea1n c\u1ed1 g\u1eafng s\u1eed d\u1ee5ng ch\u00fang \u1edf n\u01a1i mong \u0111\u1ee3i m\u1ed9t gi\u00e1 tr\u1ecb c\u1ee5 th\u1ec3.<\/span><\/p>\n<h3><b>TypeScript l\u00e0 g\u00ec v\u00e0 n\u00f3 kh\u00e1c v\u1edbi JavaScript nh\u01b0 th\u1ebf n\u00e0o?<\/b><b><\/b><\/h3>\n<p><strong>TypeScript l\u00e0 m\u1ed9t superset<\/strong><span style=\"font-weight: 400;\"><strong> (t\u1eadp m\u1eabu) c\u1ee7a JavaScript<\/strong>, c\u00f3 th\u1ec3 bi\u00ean d\u1ecbch sang JavaScript thu\u1ea7n. V\u1ec1 m\u1eb7t kh\u00e1i ni\u1ec7m, m\u1ed1i quan h\u1ec7 gi\u1eefa TypeScript v\u00e0 JavaScript c\u00f3 th\u1ec3 so s\u00e1nh v\u1edbi m\u1ed1i quan h\u1ec7 c\u1ee7a SASS v\u00e0 CSS. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">N\u00f3i c\u00e1ch kh\u00e1c, TypeScript l\u00e0 phi\u00ean b\u1ea3n ES6 c\u1ee7a JavaScript v\u1edbi m\u1ed9t s\u1ed1 t\u00ednh n\u0103ng b\u1ed5 sung.<\/span><\/p>\n<figure id=\"attachment_24376\" aria-describedby=\"caption-attachment-24376\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-24376 size-large\" src=\"\/blog\/wp-content\/uploads\/2022\/09\/TSvJS-640x575.jpg\" alt=\"typescript-la-gi-2\" width=\"640\" height=\"575\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/TSvJS.jpg 640w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/TSvJS-300x269.jpg 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/TSvJS-200x180.jpg 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/TSvJS-100x90.jpg 100w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/TSvJS-501x450.jpg 501w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-24376\" class=\"wp-caption-text\">Ngu\u1ed3n @serokell.io<\/figcaption><\/figure>\n<p><span style=\"font-weight: 400;\">TypeScript l\u00e0 m\u1ed9t <\/span><b>ng\u00f4n ng\u1eef nh\u1eadp t\u0129nh<\/b> <b>(<\/b><b>statical typed)<\/b><span style=\"font-weight: 400;\"> v\u00e0<\/span> <b>h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (object oriented)<\/b><span style=\"font-weight: 400;\">, t\u01b0\u01a1ng t\u1ef1 nh\u01b0 Java v\u00e0 C#. Trong khi \u0111\u00f3, JavaScript l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef k\u1ecbch b\u1ea3n (<\/span><span style=\"font-weight: 400;\">scripting language)<\/span><span style=\"font-weight: 400;\"> g\u1ea7n gi\u1ed1ng Python. B\u1ea3n ch\u1ea5t h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a TypeScript tr\u1edf n\u00ean ho\u00e0n thi\u1ec7n v\u1edbi c\u00e1c t\u00ednh n\u0103ng nh\u01b0 <\/span><b>class <\/b><span style=\"font-weight: 400;\">v\u00e0 <\/span><b>interface<\/b><span style=\"font-weight: 400;\">. T\u00ednh n\u0103ng nh\u1eadp t\u0129nh c\u1ee7a TypeScript cho ph\u00e9p hi\u1ec7u ch\u1ec9nh t\u1ed1t h\u01a1n th\u00f4ng qua vi\u1ec7c suy lu\u1eadn ki\u1ec3u <\/span><span style=\"font-weight: 400;\">(type inference<\/span><span style=\"font-weight: 400;\">)<\/span><span style=\"font-weight: 400;\"> tu\u1ef3 theo \u00fd c\u1ee7a b\u1ea1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u1ec1 m\u1eb7t code, TypeScript \u0111\u01b0\u1ee3c vi\u1ebft trong t\u1ec7p c\u00f3 \u0111u\u00f4i <\/span><b>.ts<\/b><span style=\"font-weight: 400;\">, trong khi JavaScript c\u00f3 \u0111u\u00f4i <\/span><b>.js<\/b><span style=\"font-weight: 400;\">. Kh\u00f4ng gi\u1ed1ng nh\u01b0 JavaScript, tr\u00ecnh duy\u1ec7t s\u1ebd kh\u00f4ng th\u1ec3 hi\u1ec3u \u0111\u01b0\u1ee3c code trong TypeScript, lo\u1ea1i code n\u00e0y c\u0169ng kh\u00f4ng th\u1ec3 th\u1ef1c thi tr\u1ef1c ti\u1ebfp trong tr\u00ecnh duy\u1ec7t ho\u1eb7c b\u1ea5t k\u1ef3 n\u1ec1n t\u1ea3ng n\u00e0o kh\u00e1c.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Do \u0111\u00f3, tr\u01b0\u1edbc ti\u00ean, c\u00e1c t\u1ec7p <\/span><b>.ts <\/b><span style=\"font-weight: 400;\">c\u1ea7n ph\u1ea3i \u0111\u01b0\u1ee3c bi\u00ean d\u1ecbch sang JavaScript thu\u1ea7n, th\u00f4ng qua tr\u00ecnh bi\u00ean d\u1ecbch <\/span><b>tsc<\/b><span style=\"font-weight: 400;\"> c\u1ee7a TypeScript. Sau \u0111\u00f3, ch\u00fang s\u1ebd \u0111\u01b0\u1ee3c th\u1ef1c thi b\u1edfi n\u1ec1n t\u1ea3ng \u0111\u00edch.<\/span><\/p>\n<blockquote><p><i><span style=\"font-weight: 400;\">Xem th\u00eam: <\/span><\/i><strong><a href=\"\/blog\/typescript-vs-javascript\/\" target=\"_blank\" rel=\"noopener\"><i>TypeScript vs JavaScript: T\u00ednh n\u0103ng v\u00e0 tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng<\/i><\/a><\/strong><\/p><\/blockquote>\n<h3><b>L\u1ee3i \u00edch c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<blockquote><p><b><i>Tips: <\/i><\/b><i><span style=\"font-weight: 400;\">M\u1ed7i ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh \u0111\u1ec1u c\u00f3 \u01b0u &#8211; nh\u01b0\u1ee3c \u0111i\u1ec3m v\u00e0 l\u00fd do ch\u00fang \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng. C\u00e2u h\u1ecfi n\u00e0y nh\u1eb1m \u0111\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1 tr\u1ea3i nghi\u1ec7m c\u00e1 nh\u00e2n c\u1ee7a dev khi s\u1eed d\u1ee5ng TypeScript, c\u0169ng nh\u01b0 c\u00e1ch n\u00f3 \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn quy tr\u00ecnh l\u00e0m vi\u1ec7c c\u1ee7a d\u1ef1 \u00e1n. Do \u0111\u00f3, thay v\u00ec ch\u1ec9 li\u1ec7t k\u00ea c\u00e1c l\u1ee3i \u00edch c\u1ee7a TypeScript, h\u00e3y d\u1eabn ch\u1ee9ng r\u1eb1ng m\u1ed9t d\u1ef1 \u00e1n \u0111i\u1ec3n h\u00ecnh c\u00f3 th\u1ec3 h\u01b0\u1edfng l\u1ee3i g\u00ec t\u1eeb n\u00f3, v\u00ed d\u1ee5 nh\u01b0 kh\u1ea3 n\u0103ng b\u1ea3o tr\u00ec t\u1ed1t h\u01a1n trong th\u1eddi gian d\u00e0i, ho\u1eb7c gi\u00fap developer t\u0103ng n\u0103ng su\u1ea5t l\u00e0m vi\u1ec7c.<\/span><\/i><\/p><\/blockquote>\n<p><b>Tr\u1ea3 l\u1eddi:\u00a0<\/b><\/p>\n<p><span style=\"font-weight: 400;\"><strong>M\u1ed9t l\u1ee3i th\u1ebf d\u1ec5 th\u1ea5y c\u1ee7a TypeScript l\u00e0 tooling.<\/strong> TypeScript l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef <\/span><b>strong typing <\/b><span style=\"font-weight: 400;\">(ngh\u0129a l\u00e0 <\/span><span style=\"font-weight: 400;\">d\u1ea1ng c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c gi\u1eef nguy\u00ean tr\u1eeb khi c\u00f3 l\u1ec7nh r\u00f5 r\u00e0ng y\u00eau c\u1ea7u thay \u0111\u1ed5i) v\u00e0<\/span><span style=\"font-weight: 400;\"> s\u1eed d\u1ee5ng <\/span><b>type inference<\/b><span style=\"font-weight: 400;\"> (suy lu\u1eadn ki\u1ec3u). Nh\u1eefng \u0111\u1eb7c \u0111i\u1ec3m n\u00e0y gi\u00fap tooling t\u1ed1t h\u01a1n v\u00e0 t\u00edch h\u1ee3p ch\u1eb7t ch\u1ebd h\u01a1n v\u1edbi c\u00e1c tr\u00ecnh s\u1eeda code. S\u1ef1 ki\u1ec3m tra nghi\u00eam ng\u1eb7t c\u1ee7a TypeScript gi\u00fap ph\u00e1t hi\u1ec7n s\u1edbm c\u00e1c l\u1ed7i, gi\u1ea3m \u0111\u00e1ng k\u1ec3 kh\u1ea3 n\u0103ng m\u1eafc l\u1ed7i ch\u00ednh t\u1ea3 v\u00e0 c\u00e1c l\u1ed7i kh\u00e1c do con ng\u01b0\u1eddi g\u00e2y ra.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T\u1eeb g\u00f3c \u0111\u1ed9 c\u1ee7a IDE (Integrated development environment &#8211; M\u00f4i tr\u01b0\u1eddng ph\u00e1t tri\u1ec3n t\u00edch h\u1ee3p), TypeScript gi\u00fap IDE hi\u1ec3u code t\u1ed1t h\u01a1n b\u1eb1ng c\u00e1ch cho ph\u00e9p n\u00f3 hi\u1ec3n th\u1ecb g\u1ee3i \u00fd, c\u1ea3nh b\u00e1o v\u00e0 l\u1ed7i sai r\u00f5 r\u00e0ng h\u01a1n \u0111\u1ebfn developer.\u00a0<\/span><\/p>\n<p><i><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: TypeScript ki\u1ec3m tra null v\u00e0 b\u00e1o l\u1ed7i ngay t\u1ea1i th\u1eddi \u0111i\u1ec3m bi\u00ean d\u1ecbch (v\u00e0 trong IDE c\u1ee7a b\u1ea1n), nh\u1edd \u0111\u00f3 ng\u0103n ch\u1eb7n m\u1ed9t l\u1ed7i ph\u1ed5 bi\u1ebfn trong JavaScript \u0111\u00f3 l\u00e0 truy c\u1eadp v\u00e0o thu\u1ed9c t\u00ednh c\u1ee7a m\u1ed9t bi\u1ebfn kh\u00f4ng x\u00e1c \u0111\u1ecbnh trong th\u1eddi gian ch\u1ea1y.<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400;\"><strong>L\u1ee3i \u00edch l\u00e2u d\u00e0i c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng TypeScript l\u00e0 kh\u1ea3 n\u0103ng m\u1edf r\u1ed9ng v\u00e0 kh\u1ea3 n\u0103ng b\u1ea3o tr\u00ec.<\/strong> Kh\u1ea3 n\u0103ng m\u00f4 t\u1ea3 c\u1ee5 th\u1ec3 c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng v\u00e0 h\u00e0m tr\u1ef1c ti\u1ebfp trong code gi\u00fap cho codebase c\u1ee7a b\u1ea1n tr\u1edf n\u00ean d\u1ec5 hi\u1ec3u h\u01a1n, d\u1ec5 d\u1ef1 \u0111o\u00e1n h\u01a1n. Khi \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u00fang c\u00e1ch, TypeScript cung c\u1ea5p m\u1ed9t ng\u00f4n ng\u1eef chu\u1ea9n h\u00f3a gi\u00fap developer \u0111\u1ecdc code t\u1ed1t h\u01a1n, t\u1eeb \u0111\u00f3 c\u00f3 th\u1ec3 ti\u1ebft ki\u1ec7m th\u1eddi gian v\u00e0 c\u00f4ng s\u1ee9c khi codebase ti\u1ebfp t\u1ee5c ph\u00e1t tri\u1ec3n.<\/span><\/p>\n<figure id=\"attachment_24377\" aria-describedby=\"caption-attachment-24377\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-24377 size-large\" src=\"\/blog\/wp-content\/uploads\/2022\/09\/pros-cons-of-typescript-1-640x531.jpg\" alt=\"typescript-la-gi-3\" width=\"640\" height=\"531\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/pros-cons-of-typescript-1.jpg 640w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/pros-cons-of-typescript-1-300x249.jpg 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/pros-cons-of-typescript-1-200x166.jpg 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/pros-cons-of-typescript-1-100x83.jpg 100w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/09\/pros-cons-of-typescript-1-542x450.jpg 542w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption id=\"caption-attachment-24377\" class=\"wp-caption-text\">B\u1ea3ng t\u00f3m t\u1eaft \u01b0u \u0111i\u1ec3m v\u00e0 nh\u01b0\u1ee3c \u0111i\u1ec3m c\u1ee7a TypeScript. Ngu\u1ed3n @appventurez<\/figcaption><\/figure>\n<h3><b>Interface trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<blockquote><p><i><span style=\"font-weight: 400;\"><strong>Tips:<\/strong> T\u01b0\u01a1ng t\u1ef1 c\u00e2u h\u1ecfi v\u1ec1 t\u00ednh n\u0103ng, c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript n\u00e0y ki\u1ec3m tra \u0111\u1ed9 hi\u1ec3u bi\u1ebft v\u00e0 quen thu\u1ed9c c\u1ee7a \u1ee9ng vi\u00ean \u0111\u1ed1i v\u1edbi ng\u00f4n ng\u1eef. M\u1ed9t c\u00e2u tr\u1ea3 l\u1eddi l\u00fd t\u01b0\u1edfng c\u1ea7n bao g\u1ed3m c\u00e1ch s\u1eed d\u1ee5ng v\u00e0 l\u1ee3i \u00edch c\u1ee7a interface trong TypeScript.<\/span><\/i><\/p><\/blockquote>\n<p><b>Interface<\/b><span style=\"font-weight: 400;\"> l\u00e0 c\u00e1ch TypeScript x\u00e1c \u0111\u1ecbnh c\u00fa ph\u00e1p c\u1ee7a c\u00e1c th\u1ef1c th\u1ec3. N\u00f3i c\u00e1ch kh\u00e1c, interface l\u00e0 m\u1ed9t c\u00e1ch \u0111\u1ec3 m\u00f4 t\u1ea3 c\u00e1c data shape nh\u01b0 objects (\u0111\u1ed1i t\u01b0\u1ee3ng) ho\u1eb7c array of objects (m\u1ea3ng \u0111\u1ed1i t\u01b0\u1ee3ng).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 thi\u1ebft l\u1eadp interface b\u1eb1ng t\u1eeb kh\u00f3a <\/span><b>interface<\/b><span style=\"font-weight: 400;\">, ti\u1ebfp \u0111\u1ebfn l\u00e0 t\u00ean v\u00e0 \u0111\u1ecbnh ngh\u0129a. C\u00f9ng xem c\u00e1ch thi\u1ebft l\u1eadp m\u1ed9t interface \u0111\u01a1n gi\u1ea3n cho object \u201cUser\u201d:<\/span><\/p>\n<pre style=\"text-align: left;\"><b>interface<\/b><span style=\"font-weight: 400;\"> User {<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0name: string;<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0age: number;<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Sau \u0111\u00f3, interface n\u00e0y c\u00f3 th\u1ec3 d\u00f9ng \u0111\u1ec3 \u0111\u1eb7t ki\u1ec3u cho m\u1ed9t bi\u1ebfn (t\u01b0\u01a1ng t\u1ef1 nh\u01b0 c\u00e1ch b\u1ea1n g\u00e1n ki\u1ec3u nguy\u00ean th\u1ee7y &#8211; primitive type cho bi\u1ebfn). Khi \u0111\u00f3, m\u1ed9t bi\u1ebfn c\u00f3 type \u201cUser\u201d s\u1ebd tu\u00e2n theo c\u00e1c thu\u1ed9c t\u00ednh c\u1ee7a interface.<\/span><\/p>\n<pre style=\"text-align: left;\"><b>let<\/b><span style=\"font-weight: 400;\"> user: User = {<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0name: \"Bob\",<\/span>\n\n<span style=\"font-weight: 400;\">\u00a0\u00a0age: 20, <\/span><i><span style=\"font-weight: 400;\">\/\/ omitting the `age` property or a assigning a different type instead of a number would throw an error<\/span><\/i>\n\n<span style=\"font-weight: 400;\">};<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">C\u00e1c <strong>interface<\/strong> gi\u00fap th\u00fac \u0111\u1ea9y t\u00ednh nh\u1ea5t qu\u00e1n trong d\u1ef1 \u00e1n TypeScript. Th\u00eam n\u1eefa, c\u00e1c <strong>interface<\/strong> c\u0169ng gi\u00fap c\u1ea3i thi\u1ec7n tooling c\u1ee7a d\u1ef1 \u00e1n, cung c\u1ea5p ch\u1ee9c n\u0103ng autocomplete trong IDE t\u1ed1t h\u01a1n v\u00e0 \u0111\u1ea3m b\u1ea3o c\u00e1c gi\u00e1 tr\u1ecb \u0111\u00fang \u0111ang \u0111\u01b0\u1ee3c truy\u1ec1n v\u00e0o constructor v\u00e0 h\u00e0m.<\/span><\/p>\n<h3><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ea1o type m\u1edbi b\u1eb1ng m\u1ed9t t\u1eadp h\u1ee3p con c\u1ee7a interface?<\/b><\/h3>\n<blockquote><p><i><span style=\"font-weight: 400;\"><strong>Tips:<\/strong> Vi\u1ec7c s\u1eeda \u0111\u1ed5i interface r\u1ea5t c\u00f3 l\u1ee3i \u0111\u1ec3 lo\u1ea1i b\u1ecf code tr\u00f9ng l\u1eb7p v\u00e0 t\u1ed1i \u0111a ho\u00e1 kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng c\u1ee7a c\u00e1c interface hi\u1ec7n c\u00f3 (n\u1ebfu \u0111i\u1ec1u \u0111\u00f3 h\u1ee3p l\u00fd). C\u00e2u h\u1ecfi n\u00e0y nh\u1eafc \u0111\u1ebfn m\u1ed9t trong nhi\u1ec1u t\u00ednh n\u0103ng m\u00e0 TypeScript cung c\u1ea5p \u0111\u1ec3 t\u1ea1o ra m\u1ed9t interface m\u1edbi t\u1eeb interface hi\u1ec7n c\u00f3.<\/span><\/i><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">TypeScript c\u00f3 m\u1ed9t utility type g\u1ecdi l\u00e0 <\/span><b>\u201comit\u201d <\/b><span style=\"font-weight: 400;\">(b\u1ecf qua), cho ph\u00e9p b\u1ea1n t\u1ea1o ra m\u1ed9t type m\u1edbi b\u1eb1ng c\u00e1ch pass m\u1ed9t type\/ interface \u0111ang c\u00f3 v\u00e0 ch\u1ecdn l\u1ef1a c\u00e1c key s\u1ebd \u0111\u01b0\u1ee3c lo\u1ea1i b\u1ecf kh\u1ecfi type m\u1edbi.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5 d\u01b0\u1edbi \u0111\u00e2y cho th\u1ea5y c\u00e1ch t\u1ea1o ra type m\u1edbi \u201cUserPreview\u201d d\u1ef1a tr\u00ean interface \u201cUser\u201d \u0111\u00e3 c\u00f3, nh\u01b0ng \u0111\u00e3 \u0111\u01b0\u1ee3c lo\u1ea1i b\u1ecf thu\u1ed9c t\u00ednh email:<\/span><\/p>\n<pre style=\"text-align: left;\"><b>interface<\/b><span style=\"font-weight: 400;\"> User {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0name: string;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0description: string;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0age: number;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0email: string;<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<i><span style=\"font-weight: 400;\">\/\/ removes the `email` property from the User interface<\/span><\/i>\n\n<b>type<\/b><span style=\"font-weight: 400;\"> UserPreview = Omit&lt;User, \"email\"&gt;;<\/span>\n\n<b>const<\/b><span style=\"font-weight: 400;\"> userPreview: UserPreview = {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0name: \"Bob\",<\/span>\n<span style=\"font-weight: 400;\"> \u00a0description: \"Awesome guy\",<\/span>\n<span style=\"font-weight: 400;\"> \u00a0age: 20,<\/span>\n<span style=\"font-weight: 400;\">};<\/span><\/pre>\n<h3><b>C\u00e1c \u201cenum\u201d ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o trong TypeScript?<\/b><\/h3>\n<blockquote><p><i><span style=\"font-weight: 400;\"><strong>Tips<\/strong>: Enum l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ph\u1ed5 bi\u1ebfn trong h\u1ea7u h\u1ebft c\u00e1c ng\u00f4n ng\u1eef typed. Hi\u1ec3u r\u00f5 v\u1ec1 enum v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng ch\u00fang l\u00e0 m\u1ed9t ph\u1ea7n quan tr\u1ecdng trong vi\u1ec7c t\u1ed5 ch\u1ee9c code v\u00e0 gi\u00fap code tr\u1edf n\u00ean d\u1ec5 \u0111\u1ecdc h\u01a1n. B\u1ea1n c\u1ea7n gi\u1ea3i th\u00edch v\u1ec1 enum \u1edf c\u1ea5p \u0111\u1ed9 cao, \u0111\u1ed3ng th\u1eddi tr\u00ecnh b\u00e0y \u0111\u01b0\u1ee3c c\u00e1c t\u00ednh n\u0103ng v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng c\u01a1 b\u1ea3n c\u1ee7a ch\u00fang.<\/span><\/i><\/p><\/blockquote>\n<p><b>Enum<\/b><span style=\"font-weight: 400;\"> &#8211; hay c\u00f2n g\u1ecdi l\u00e0 <\/span><b>enumerated types<\/b> <span style=\"font-weight: 400;\">(<\/span><span style=\"font-weight: 400;\">ki\u1ec3u li\u1ec7t k\u00ea) l\u00e0 m\u1ed9t ph\u01b0\u01a1ng ti\u1ec7n \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c h\u1eb1ng s\u1ed1 \u0111\u01b0\u1ee3c \u0111\u1eb7t t\u00ean. C\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u n\u00e0y c\u00f3 \u0111\u1ed9 d\u00e0i kh\u00f4ng \u0111\u1ed5i v\u00e0 ch\u1ee9a m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c gi\u00e1 tr\u1ecb kh\u00f4ng \u0111\u1ed5i. Enum trong TypeScript th\u01b0\u1eddng \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 bi\u1ec3u di\u1ec5n m\u1ed9t s\u1ed1 l\u01b0\u1ee3ng nh\u1ea5t \u0111\u1ecbnh c\u00e1c t\u00f9y ch\u1ecdn cho m\u1ed9t gi\u00e1 tr\u1ecb cho tr\u01b0\u1edbc, th\u00f4ng qua m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c c\u1eb7p key\/ value.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">H\u00e3y xem v\u00ed d\u1ee5 v\u1ec1 <\/span><b>enum <\/b><span style=\"font-weight: 400;\">d\u00f9ng \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c \u201cuser type\u201d<\/span><\/p>\n<pre>enum UserType {\n \u00a0Guest = \"G\",\n \u00a0Verified = \"V\",\n \u00a0Admin = \"A\",\n}\nconst userType: UserType = UserType.Verified;<\/pre>\n<p><span style=\"font-weight: 400;\">\u1ede b\u00ean d\u01b0\u1edbi, TypeScript s\u1ebd bi\u00ean d\u1ecbch c\u00e1c<\/span><b> enum <\/b><span style=\"font-weight: 400;\">th\u00e0nh c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng JavaScript thu\u1ea7n. \u0110i\u1ec1u n\u00e0y khi\u1ebfn vi\u1ec7c s\u1eed d\u1ee5ng enum c\u00f3 l\u1ee3i h\u01a1n so v\u1edbi s\u1eed d\u1ee5ng nhi\u1ec1u bi\u1ebfn <\/span><b>const<\/b><span style=\"font-weight: 400;\"> \u0111\u1ed9c l\u1eadp. Nh\u00f3m m\u00e0 enum t\u1ea1o ra gi\u00fap cho code c\u1ee7a b\u1ea1n an to\u00e0n v\u1ec1 type v\u00e0 d\u1ec5 \u0111\u1ecdc h\u01a1n.<\/span><\/p>\n<h3><b>H\u00e0m arrow (h\u00e0m m\u0169i t\u00ean) trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<blockquote><p><i><span style=\"font-weight: 400;\"><strong>Tips:<\/strong> Arrow function l\u00e0 m\u1ed9t t\u00ednh n\u0103ng ph\u1ed5 bi\u1ebfn c\u1ee7a ES6 v\u00e0 TypeScript nh\u1eb1m gi\u1edbi thi\u1ec7u c\u00e1ch kh\u00e1c ng\u1eafn h\u01a1n \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh c\u00e1c h\u00e0m. Arrow function c\u0169ng c\u00f3 nh\u1eefng \u01b0u v\u00e0 nh\u01b0\u1ee3c \u0111i\u1ec3m quan tr\u1ecdng m\u00e0 b\u1ea1n c\u1ea7n xem x\u00e9t khi l\u1ef1a ch\u1ecdn ph\u01b0\u01a1ng ph\u00e1p n\u00e0o s\u1ebd \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng.<\/span><\/i><\/p><\/blockquote>\n<p><b>H\u00e0m arrow<\/b><span style=\"font-weight: 400;\"> &#8211; hay c\u00f2n \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 <\/span><b>h\u00e0m lambda<\/b><span style=\"font-weight: 400;\">, cung c\u1ea5p m\u1ed9t c\u00fa ph\u00e1p ng\u1eafn g\u1ecdn v\u00e0 thu\u1eadn ti\u1ec7n \u0111\u1ec3 khai b\u00e1o c\u00e1c h\u00e0m. C\u00e1c h\u00e0m arrow th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u1ea1o c\u00e1c <\/span><b>h\u00e0m g\u1ecdi l\u1ea1i (callback function)<\/b><span style=\"font-weight: 400;\"> trong TypeScript. C\u00e1c ph\u00e9p to\u00e1n m\u1ea3ng (array operations) nh\u01b0 <\/span><b>map, filter,<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>reduce <\/b><span style=\"font-weight: 400;\">\u0111\u1ec1u ch\u1ea5p nh\u1eadn c\u00e1c h\u00e0m arrow<\/span> <span style=\"font-weight: 400;\">l\u00e0m \u0111\u1ed1i s\u1ed1 c\u1ee7a ch\u00fang.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Tuy nhi\u00ean, t\u00ednh \u1ea9n danh c\u1ee7a h\u00e0m arrow c\u0169ng c\u00f3 m\u1eb7t tr\u00e1i. N\u1ebfu d\u00f9ng kh\u00f4ng \u0111\u00fang, c\u00fa ph\u00e1p ng\u1eafn h\u01a1n c\u00f3 th\u1ec3 g\u00e2y kh\u00f3 hi\u1ec3u h\u01a1n. H\u01a1n n\u1eefa, b\u1ea3n ch\u1ea5t kh\u00f4ng t\u00ean c\u1ee7a c\u00e1c h\u00e0m arrow c\u0169ng khi\u1ebfn n\u00f3 kh\u00f4ng th\u1ec3 t\u1ea1o c\u00e1c h\u00e0m t\u1ef1 tham chi\u1ebfu (t\u1ee9c l\u00e0 \u0111\u1ec7 quy).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ch\u00fang ta h\u00e3y xem c\u00e1ch m\u1ed9t <\/span><b>h\u00e0m ch\u00ednh quy (regular function)<\/b><span style=\"font-weight: 400;\"> ch\u1ea5p nh\u1eadn hai s\u1ed1 v\u00e0 tr\u1ea3 v\u1ec1 t\u1ed5ng c\u1ee7a n\u00f3.<\/span><\/p>\n<pre>function addNumbers(x: number, y: number): number {\n \u00a0return x + y;\n}\naddNumbers(1, 2); \/\/ returns 3<\/pre>\n<p><span style=\"font-weight: 400;\">B\u00e2y gi\u1edd, h\u00e3y chuy\u1ec3n \u0111\u1ed5i h\u00e0m tr\u00ean th\u00e0nh m\u1ed9t <\/span><b>h\u00e0m arrow<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<pre>const addNumbers = (x: number, y: number): number =&gt; {\n \u00a0return x + y;\n};\naddNumbers(1, 2); \/\/ returns 3<\/pre>\n<h3><b>S\u1ef1 kh\u00e1c nhau gi\u1eefa var, let v\u00e0 const trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<blockquote><p><b><i>Tips: <\/i><\/b><i><span style=\"font-weight: 400;\">TypeScript cung c\u1ea5p ba c\u00e1ch khai b\u00e1o bi\u1ebfn kh\u00e1c nhau: var, let v\u00e0 const. Ph\u00e2n bi\u1ec7t \u0111\u01b0\u1ee3c ba t\u1eeb kh\u00f3a n\u00e0y, hi\u1ec3u \u0111\u01b0\u1ee3c khi n\u00e0o n\u00ean d\u00f9ng t\u1eeb n\u00e0o l\u00e0 y\u00eau c\u1ea7u quan tr\u1ecdng \u0111\u1ec3 vi\u1ebft code ch\u1ea5t l\u01b0\u1ee3ng. H\u00e3y \u0111\u1ea3m b\u1ea3o r\u1eb1ng b\u1ea1n c\u00f3 tr\u00ecnh b\u00e0y v\u1ec1 c\u00e1c tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng c\u1ee5 th\u1ec3 \u0111\u1ed1i v\u1edbi t\u1eebng t\u1eeb kh\u00f3a, k\u00e8m theo t\u00ednh ch\u1ea5t c\u1ee7a ch\u00fang.<\/span><\/i><\/p><\/blockquote>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>var:<\/b><span style=\"font-weight: 400;\"> Khai b\u00e1o bi\u1ebfn function scope ho\u1eb7c bi\u1ebfn to\u00e0n c\u1ee5c (global scope), c\u00f3 t\u00ednh ch\u1ea5t v\u00e0 quy t\u1eafc ph\u1ea1m vi t\u01b0\u01a1ng t\u1ef1 v\u1edbi c\u00e1c bi\u1ebfn <\/span><b>var<\/b><span style=\"font-weight: 400;\"> c\u1ee7a JavaScript. C\u00e1c bi\u1ebfn <\/span><b>var<\/b><span style=\"font-weight: 400;\"> kh\u00f4ng y\u00eau c\u1ea7u g\u00e1n gi\u00e1 tr\u1ecb cho n\u00f3 trong qu\u00e1 tr\u00ecnh khai b\u00e1o.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>let:<\/b><span style=\"font-weight: 400;\"> Khai b\u00e1o m\u1ed9t bi\u1ebfn c\u1ee5c b\u1ed9 trong ph\u1ea1m vi kh\u1ed1i (block-scoped local variable). C\u00e1c bi\u1ebfn <\/span><b>let <\/b><span style=\"font-weight: 400;\">kh\u00f4ng y\u00eau c\u1ea7u g\u00e1n gi\u00e1 tr\u1ecb cho m\u1ed9t bi\u1ebfn trong qu\u00e1 tr\u00ecnh khai b\u00e1o. Block-scoped local variable c\u00f3 ngh\u0129a l\u00e0 bi\u1ebfn ch\u1ec9 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp trong kh\u1ed1i ch\u1ee9a c\u1ee7a n\u00f3, ch\u1eb3ng h\u1ea1n nh\u01b0 m\u1ed9t h\u00e0m, m\u1ed9t kh\u1ed1i if\/ else ho\u1eb7c m\u1ed9t v\u00f2ng l\u1eb7p. H\u01a1n n\u1eefa, kh\u00f4ng gi\u1ed1ng nh\u01b0 <\/span><b>var<\/b><span style=\"font-weight: 400;\">, ch\u00fang ta kh\u00f4ng th\u1ec3 \u0111\u1ecdc ho\u1eb7c ghi bi\u1ebfn <\/span><b>let<\/b><span style=\"font-weight: 400;\"> tr\u01b0\u1edbc khi ch\u00fang \u0111\u01b0\u1ee3c khai b\u00e1o.<\/span><\/li>\n<\/ul>\n<pre>\/\/ reading\/writing before a `let` variable is declared\n\nconsole.log(\"age\", age); \/\/ Compiler Error: error TS2448: Block-scoped variable 'age' used before its declaration\nage = 20; \/\/ Compiler Error: error TS2448: Block-scoped variable 'age' used before its declaration\nlet age: number;\n\n\/\/ accessing `let` variable outside it's scope\n\nfunction user(): void {\n \u00a0let name: string;\n \u00a0if (true) {\n \u00a0\u00a0\u00a0let email: string;\n \u00a0\u00a0\u00a0console.log(name); \/\/ OK\n \u00a0\u00a0\u00a0console.log(email); \/\/ OK\n \u00a0}\n \u00a0console.log(name); \/\/ OK\n \u00a0console.log(email); \/\/ Compiler Error: Cannot find name 'email'\n}<\/pre>\n<ul>\n<li><b>const:<\/b><span style=\"font-weight: 400;\"> Khai b\u00e1o m\u1ed9t gi\u00e1 tr\u1ecb h\u1eb1ng s\u1ed1 trong ph\u1ea1m vi kh\u1ed1i m\u00e0 gi\u00e1 tr\u1ecb \u0111\u00f3 kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i sau khi kh\u1edfi t\u1ea1o. C\u00e1c bi\u1ebfn <\/span><b>const <\/b><span style=\"font-weight: 400;\">y\u00eau c\u1ea7u vi\u1ec7c kh\u1edfi t\u1ea1o nh\u01b0 m\u1ed9t ph\u1ea7n trong qu\u00e1 tr\u00ecnh khai b\u00e1o. \u0110i\u1ec1u n\u00e0y tr\u1edf n\u00ean l\u00fd t\u01b0\u1edfng \u0111\u1ed1i v\u1edbi c\u00e1c bi\u1ebfn kh\u00f4ng thay \u0111\u1ed5i trong su\u1ed1t th\u1eddi gian t\u1ed3n t\u1ea1i c\u1ee7a ch\u00fang.<\/span><\/li>\n<\/ul>\n<pre>\/\/ reassigning a `const` variable\n\nconst age: number = 20;\nage = 30; \/\/ Compiler Error: Cannot assign to 'age' because it is a constant or read-only property\n\n\/\/ declaring a `const` variable without initialization\n\nconst name: string; \/\/ Compiler Error: const declaration must be initialized<\/pre>\n<h3><b>Khi n\u00e0o b\u1ea1n s\u1eed d\u1ee5ng return type l\u00e0 \u201cnever\u201d v\u00e0 n\u00f3 kh\u00e1c v\u1edbi \u201cvoid\u201d nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<blockquote><p><i><span style=\"font-weight: 400;\"><strong>Tips:<\/strong> \u0110\u00e2y l\u00e0 c\u00e2u h\u1ecfi th\u1ec3 hi\u1ec7n hi\u1ec3u bi\u1ebft c\u1ee7a b\u1ea1n v\u1ec1 c\u00e1c \u201ctype\u201d trong TypeScript v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng ch\u00fang trong vi\u1ec7c x\u00e1c \u0111\u1ecbnh return type. B\u1ea1n c\u1ea7n ph\u00e2n bi\u1ec7t gi\u1eefa hai type v\u00e0 x\u00e1c \u0111\u1ecbnh \u0111\u01b0\u1ee3c return type c\u1ee7a m\u1ed9t h\u00e0m th\u00f4ng qua vi\u1ec7c xem n\u1ed9i dung c\u1ee7a n\u00f3.<\/span><\/i><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">Tr\u01b0\u1edbc khi \u0111i s\u00e2u v\u00e0o s\u1ef1 kh\u00e1c bi\u1ec7t gi\u1eefa <\/span><b>never<\/b><span style=\"font-weight: 400;\"> v\u00e0 <\/span><b>void<\/b><span style=\"font-weight: 400;\">, c\u00f9ng n\u00f3i v\u1ec1 ho\u1ea1t \u0111\u1ed9ng c\u1ee7a m\u1ed9t h\u00e0m JavaScript khi kh\u00f4ng c\u00f3 g\u00ec \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 r\u00f5 r\u00e0ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">H\u00e3y xem qua h\u00e0m trong v\u00ed d\u1ee5 b\u00ean d\u01b0\u1edbi. N\u00f3 kh\u00f4ng tr\u1ea3 l\u1ea1i b\u1ea5t c\u1ee9 \u0111i\u1ec1u g\u00ec r\u00f5 r\u00e0ng cho caller (h\u00e0m g\u1ecdi). Tuy nhi\u00ean, n\u1ebfu b\u1ea1n g\u00e1n n\u00f3 cho m\u1ed9t bi\u1ebfn v\u00e0 ghi l\u1ea1i gi\u00e1 tr\u1ecb c\u1ee7a bi\u1ebfn \u0111\u00f3, b\u1ea1n s\u1ebd th\u1ea5y r\u1eb1ng gi\u00e1 tr\u1ecb c\u1ee7a h\u00e0m l\u00e0 <\/span><b>undefined<\/b><span style=\"font-weight: 400;\"> (kh\u00f4ng x\u00e1c \u0111\u1ecbnh).<\/span><\/p>\n<pre>printName(name: string): void {\n \u00a0console.log(name);\n}\nconst printer = printName('Will');\nconsole.log(printer); \/\/ logs \"undefined\"<\/pre>\n<p><span style=\"font-weight: 400;\">\u0110o\u1ea1n m\u00e3 tr\u00ean l\u00e0 m\u1ed9t v\u00ed d\u1ee5 v\u1ec1 <\/span><b>void. <\/b><span style=\"font-weight: 400;\">C\u00e1c h\u00e0m kh\u00f4ng tr\u1ea3 v\u1ec1 r\u00f5 r\u00e0ng \u0111\u01b0\u1ee3c TypeScript suy lu\u1eadn c\u00f3 return type l\u00e0 <\/span><b>void.<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Ng\u01b0\u1ee3c l\u1ea1i, <\/span><b>never<\/b><span style=\"font-weight: 400;\"> l\u00e0 h\u00e0m \u0111\u1ea1i di\u1ec7n cho m\u1ed9t gi\u00e1 tr\u1ecb kh\u00f4ng bao gi\u1edd x\u1ea3y ra. V\u00ed d\u1ee5, m\u1ed9t h\u00e0m c\u00f3 v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n ho\u1eb7c m\u1ed9t h\u00e0m b\u00e1o l\u1ed7i l\u00e0 nh\u1eefng h\u00e0m c\u00f3 return type l\u00e0 <\/span><b>never.<\/b><\/p>\n<pre>const error = (): never =&gt; {\n \u00a0throw new Error(\"\");\n};<\/pre>\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, <\/span><b>void<\/b><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi m\u1ed9t h\u00e0m kh\u00f4ng tr\u1ea3 v\u1ec1 b\u1ea5t k\u1ef3 th\u1ee9 g\u00ec r\u00f5 r\u00e0ng, c\u00f2n <\/span><b>never<\/b><span style=\"font-weight: 400;\"> \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi m\u1ed9t h\u00e0m kh\u00f4ng bao gi\u1edd tr\u1ea3 v\u1ec1.<\/span><\/p>\n<h3><b>TypeScript h\u1ed7 tr\u1ee3 nh\u1eefng Access modifier (ph\u1ea1m vi truy c\u1eadp) n\u00e0o?<\/b><\/h3>\n<blockquote><p><i><span style=\"font-weight: 400;\"><strong>Tips:<\/strong> Access modifier (ph\u1ea1m vi truy c\u1eadp) v\u00e0 encapsulation (s\u1ef1 \u0111\u00f3ng g\u00f3i) song h\u00e0nh v\u1edbi nhau. C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript n\u00e0y nh\u1eb1m \u0111\u1ec3 hi\u1ec3u ki\u1ebfn \u200b\u200bth\u1ee9c c\u1ee7a \u1ee9ng vi\u00ean v\u1ec1 kh\u00e1i ni\u1ec7m \u201c\u0111\u00f3ng g\u00f3i\u201d, c\u0169ng nh\u01b0 v\u1ec1\u00a0 c\u00e1ch ti\u1ebfp c\u1eadn c\u1ee7a TypeScript v\u1edbi vi\u1ec7c h\u1ea1n ch\u1ebf truy c\u1eadp d\u1eef li\u1ec7u. Ng\u01b0\u1eddi h\u1ecfi c\u0169ng c\u00f3 th\u1ec3 \u0111ang mu\u1ed1n nghe v\u1ec1 c\u00e1c \u1ee9ng d\u1ee5ng th\u1ef1c t\u1ebf c\u1ee7a ph\u1ea1m vi truy c\u1eadp, l\u00fd do \u0111\u1eb1ng sau v\u00e0 l\u1ee3i \u00edch c\u1ee7a ch\u00fang<\/span><\/i><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">Kh\u00e1i ni\u1ec7m <\/span><b>\u201cencapsulation\u201d<\/b><span style=\"font-weight: 400;\"> (\u0111\u00f3ng g\u00f3i) \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng, nh\u1eb1m ki\u1ec3m so\u00e1t kh\u1ea3 n\u0103ng hi\u1ec3n th\u1ecb nh\u1eefng thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c c\u1ee7a c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng. TypeScript s\u1eed d\u1ee5ng c\u00e1c <\/span><b>access modifier<\/b> <span style=\"font-weight: 400;\">\u0111\u1ec3 c\u00e0i \u0111\u1eb7t kh\u1ea3 n\u0103ng hi\u1ec3n th\u1ecb n\u1ed9i dung c\u1ee7a m\u1ed9t class (l\u1edbp). V\u00ec TypeScript \u0111\u01b0\u1ee3c bi\u00ean d\u1ecbch sang JavaScript, logic li\u00ean quan \u0111\u1ebfn ph\u1ea1m vi truy c\u1eadp \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng trong th\u1eddi gian bi\u00ean d\u1ecbch, kh\u00f4ng ph\u1ea3i trong l\u00fac ch\u1ea1y<\/span><\/p>\n<p><span style=\"font-weight: 400;\">C\u00f3 ba lo\u1ea1i <\/span><b>access modifier<\/b><span style=\"font-weight: 400;\"> trong TypeScript l\u00e0: <\/span><b>public, private, <\/b><span style=\"font-weight: 400;\">v\u00e0 <\/span><b>protected.<\/b><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>public: <\/b><span style=\"font-weight: 400;\">T\u1ea5t c\u1ea3 c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c m\u1eb7c \u0111\u1ecbnh c\u00f4ng khai. Ch\u00fang ta c\u00f3 th\u1ec3 nh\u00ecn th\u1ea5y v\u00e0 truy c\u1eadp c\u00e1c member c\u00f4ng khai c\u1ee7a m\u1ed9t class t\u1eeb b\u1ea5t k\u1ef3 v\u1ecb tr\u00ed n\u00e0o.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>protected: <\/b><span style=\"font-weight: 400;\">C\u00e1c thu\u1ed9c t\u00ednh \u0111\u01b0\u1ee3c b\u1ea3o v\u1ec7 c\u00f3 th\u1ec3 truy c\u1eadp \u0111\u01b0\u1ee3c t\u1eeb trong c\u00f9ng m\u1ed9t class v\u00e0 subclass c\u1ee7a n\u00f3.\u00a0<\/span><\/li>\n<\/ul>\n<p><i><span style=\"font-weight: 400;\">V\u00ed d\u1ee5: m\u1ed9t bi\u1ebfn ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c v\u1edbi t\u1eeb kh\u00f3a protected s\u1ebd c\u00f3 th\u1ec3 truy c\u1eadp \u0111\u01b0\u1ee3c t\u1eeb b\u1ea5t k\u1ef3 \u0111\u00e2u trong class c\u1ee7a n\u00f3, \u0111\u1ed3ng th\u1eddi trong m\u1ed9t class kh\u00e1c m\u1edf r\u1ed9ng class ch\u1ee9a bi\u1ebfn ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3.<\/span><\/i><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>private: <\/b><span style=\"font-weight: 400;\">C\u00e1c thu\u1ed9c t\u00ednh ri\u00eang t\u01b0 ch\u1ec9 c\u00f3 th\u1ec3 truy c\u1eadp \u0111\u01b0\u1ee3c t\u1eeb trong class m\u00e0 thu\u1ed9c t\u00ednh ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3 \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 s\u1eed d\u1ee5ng b\u1ea5t k\u1ef3 ph\u1ea1m vi truy c\u1eadp n\u00e0o trong s\u1ed1 tr\u00ean, h\u00e3y th\u00eam t\u1eeb kho\u00e1 <\/span><b>public, private, <\/b><span style=\"font-weight: 400;\">v\u00e0 <\/span><b>protected <\/b><span style=\"font-weight: 400;\">\u0111\u1eb1ng tr\u01b0\u1edbc thu\u1ed9c t\u00ednh ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c (n\u1ebfu b\u1ecb b\u1ecf qua, TypeScript s\u1ebd m\u1eb7c \u0111\u1ecbnh l\u00e0 <\/span><b>public<\/b><span style=\"font-weight: 400;\">)\u00a0<\/span><\/p>\n<pre>class User {\n\n \u00a0private username; \/\/ only accessible inside the `User` class\n\n\u00a0\u00a0\/\/ only accessible inside the `User` class and its subclass\n\n\u00a0\u00a0protected updateUser(): void {}\n\n\u00a0\u00a0\/\/ accessible from any location\n\n\u00a0\u00a0public getUser() {}\n\n}<\/pre>\n<p><span style=\"font-weight: 400;\">Vi ph\u1ea1m c\u00e1c quy t\u1eafc c\u1ee7a ph\u1ea1m vi truy c\u1eadp, v\u00ed d\u1ee5 nh\u01b0 c\u1ed1 g\u1eafng truy c\u1eadp thu\u1ed9c t\u00ednh private c\u1ee7a m\u1ed9t class t\u1eeb m\u1ed9t class kh\u00e1c s\u1ebd d\u1eabn \u0111\u1ebfn l\u1ed7i d\u01b0\u1edbi \u0111\u00e2y trong qu\u00e1 tr\u00ecnh bi\u00ean d\u1ecbch<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">Property '&lt;property-name&gt;' is private and only accessible within class '&lt;class-name&gt;'.<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">T\u00f3m l\u1ea1i, ph\u1ea1m vi truy c\u1eadp \u0111\u00f3ng m\u1ed9t vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c s\u1eafp x\u1ebfp code. Ch\u00fang cho ph\u00e9p hi\u1ec3n th\u1ecb m\u1ed9t t\u1eadp h\u1ee3p c\u00e1c API c\u00f4ng khai v\u00e0 \u1ea9n \u0111i c\u00e1c chi ti\u1ebft tri\u1ec3n khai. B\u1ea1n c\u00f3 th\u1ec3 xem ph\u1ea1m vi truy c\u1eadp nh\u01b0 nh\u1eefng c\u00e1nh c\u1ed5ng d\u1eabn v\u00e0o class. S\u1eed d\u1ee5ng hi\u1ec7u qu\u1ea3 c\u00e1c ph\u1ea1m vi truy c\u1eadp s\u1ebd l\u00e0m gi\u1ea3m \u0111\u00e1ng k\u1ec3 kh\u1ea3 n\u0103ng x\u1ea3y ra l\u1ed7i do s\u1eed d\u1ee5ng nh\u1ea7m n\u1ed9i dung c\u1ee7a class kh\u00e1c.<\/span><\/p>\n<h3><b>Generic l\u00e0 g\u00ec v\u00e0 c\u00e1ch s\u1eed d\u1ee5ng ch\u00fang trong TypeScript?<\/b><\/h3>\n<blockquote><p><i><span style=\"font-weight: 400;\"><strong>Tips:<\/strong> B\u00ean c\u1ea1nh \u00fd \u201cl\u00e0 g\u00ec\u201d, ng\u01b0\u1eddi h\u1ecfi c\u0169ng c\u00f3 th\u1ec3 \u0111ang mu\u1ed1n bi\u1ebft c\u00e1c h\u00e0m generic li\u00ean quan \u0111\u1ebfn t\u00ednh linh ho\u1ea1t c\u1ee7a code th\u1ebf n\u00e0o, c\u0169ng nh\u01b0 l\u00fd do \u0111\u1eb1ng sau vi\u1ec7c s\u1eed d\u1ee5ng generic thay v\u00ec c\u00e1c type kh\u00e1c. H\u00e3y chia s\u1ebb hi\u1ec3u bi\u1ebft v\u1ec1 generic l\u00e0 g\u00ec, c\u00e1ch ho\u1ea1t \u0111\u1ed9ng v\u00e0 l\u1ee3i \u00edch c\u1ee7a n\u00f3.<\/span><\/i><\/p><\/blockquote>\n<p><span style=\"font-weight: 400;\">Nh\u1eefng b\u00e0i h\u1ecdc \u201c\u0111\u1eaft gi\u00e1\u201d v\u1ec1 k\u1ef9 thu\u1eadt ph\u1ea7n m\u1ec1m th\u01b0\u1eddng khuy\u1ebfn kh\u00edch kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng v\u00e0 t\u00ednh linh ho\u1ea1t. Vi\u1ec7c s\u1eed d\u1ee5ng <\/span><b>generic<\/b><span style=\"font-weight: 400;\"> mang \u0111\u1ebfn kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng v\u00e0 t\u00ednh linh ho\u1ea1t b\u1eb1ng c\u00e1ch: Cho ph\u00e9p m\u1ed9t component ho\u1ea1t \u0111\u1ed9ng tr\u00ean nhi\u1ec1u type thay v\u00ec m\u1ed9t type duy nh\u1ea5t, m\u00e0 v\u1eabn gi\u1eef \u0111\u01b0\u1ee3c \u0111\u1ed9 ch\u00ednh x\u00e1c (kh\u00f4ng gi\u1ed1ng nh\u01b0 vi\u1ec7c s\u1eed d\u1ee5ng h\u00e0m <\/span><b>any<\/b><span style=\"font-weight: 400;\">).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5 v\u1ec1 m\u1ed9t h\u00e0m <\/span><b>generic <\/b><span style=\"font-weight: 400;\">cho ph\u00e9p caller x\u00e1c \u0111\u1ecbnh type s\u1ebd \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong h\u00e0m.<\/span><\/p>\n<pre>function updateUser&lt;Type&gt;(arg: Type): Type {\n \u00a0return arg;\n}<\/pre>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 g\u1ecdi m\u1ed9t h\u00e0m <\/span><b>generic<\/b><span style=\"font-weight: 400;\">, b\u1ea1n c\u00f3 th\u1ec3 truy\u1ec1n tr\u1ef1c ti\u1ebfp trong type th\u00f4ng qua d\u1ea5u ngo\u1eb7c nh\u1ecdn &lt;&gt; ho\u1eb7c th\u00f4ng qua <\/span><span style=\"font-weight: 400;\">type argument inference (<\/span><span style=\"font-weight: 400;\">cho ph\u00e9p TypeScript suy ra type d\u1ef1a tr\u00ean ki\u1ec3u c\u1ee7a \u0111\u1ed1i s\u1ed1 \u0111\u01b0\u1ee3c truy\u1ec1n v\u00e0o).<\/span><\/p>\n<pre>\/\/ explicitly specifying the type\nlet user = updateUser&lt;string&gt;(\"Bob\");\n\n\/\/ type argument inference\nlet user = updateUser(\"Bob\");<\/pre>\n<p><b>Generic <\/b><span style=\"font-weight: 400;\">cho ph\u00e9p ch\u00fang ta theo d\u00f5i th\u00f4ng tin type trong h\u00e0m. \u0110i\u1ec1u n\u00e0y l\u00e0m cho code tr\u1edf n\u00ean linh ho\u1ea1t v\u00e0 c\u00f3 th\u1ec3 t\u00e1i s\u1eed d\u1ee5ng m\u00e0 kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn \u0111\u1ed9 ch\u00ednh x\u00e1c c\u1ee7a lo\u1ea1i code.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_TypeScript_cho_Junior_hoac_Senior_Developer\"><\/span><b>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript cho Junior ho\u1eb7c Senior Developer\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>B\u1ea1n c\u00f3 th\u1ec3 gi\u1ea3i th\u00edch type inference trong TypeScript l\u00e0 g\u00ec kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Type inference l\u00e0 m\u1ed9t t\u00ednh n\u0103ng trong TypeScript, trong \u0111\u00f3 tr\u00ecnh bi\u00ean d\u1ecbch t\u1ef1 \u0111\u1ed9ng s\u1ebd x\u00e1c \u0111\u1ecbnh ki\u1ec3u c\u1ee7a bi\u1ebfn d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb ban \u0111\u1ea7u c\u1ee7a bi\u1ebfn \u0111\u00f3. T\u00ednh n\u0103ng n\u00e0y gi\u00fap gi\u1ea3m vi\u1ec7c s\u1eed d\u1ee5ng type annotations trong khi v\u1eabn \u0111\u1ea3m b\u1ea3o an to\u00e0n ki\u1ec3u (type safety).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5, n\u1ebfu b\u1ea1n khai b\u00e1o m\u1ed9t bi\u1ebfn v\u00e0 g\u00e1n m\u1ed9t s\u1ed1 cho bi\u1ebfn \u0111\u00f3, TypeScript s\u1ebd suy ra ki\u1ec3u c\u1ee7a bi\u1ebfn \u0111\u00f3 l\u00e0 &#8216;s\u1ed1&#8217;. \u0110i\u1ec1u n\u00e0y \u0111\u1ea3m b\u1ea3o r\u1eb1ng b\u1ea1n kh\u00f4ng th\u1ec3 v\u00f4 t\u00ecnh g\u00e1n m\u1ed9t gi\u00e1 tr\u1ecb c\u00f3 ki\u1ec3u kh\u00e1c cho bi\u1ebfn sau n\u00e0y.<\/span><\/p>\n<h3><b>S\u1ef1 kh\u00e1c bi\u1ec7t ch\u00ednh gi\u1eefa ki\u1ec3u &#8216;any&#8217; v\u00e0 &#8216;unknown&#8217; trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">S\u1ef1 kh\u00e1c nhau c\u01a1 b\u1ea3n gi\u1eefa any v\u00e0 unknown ch\u00ednh l\u00e0:\u00a0<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>any <\/b><span style=\"font-weight: 400;\">cho ph\u00e9p b\u1ea1n g\u00e1n b\u1ea5t k\u1ef3 gi\u00e1 tr\u1ecb n\u00e0o cho m\u1ed9t bi\u1ebfn m\u00e0 kh\u00f4ng c\u1ea7n ki\u1ec3m tra ki\u1ec3u d\u1eef li\u1ec7u, v\u1ec1 c\u01a1 b\u1ea3n l\u00e0 ch\u1ecdn kh\u00f4ng ki\u1ec3m tra ki\u1ec3u cho bi\u1ebfn \u0111\u00f3.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>unknown<\/b><span style=\"font-weight: 400;\"> l\u00e0 phi\u00ean b\u1ea3n an to\u00e0n h\u01a1n c\u1ee7a any. B\u1ea1n c\u00f3 th\u1ec3 g\u00e1n b\u1ea5t k\u1ef3 gi\u00e1 tr\u1ecb n\u00e0o cho unknown, nh\u01b0ng ph\u1ea3i ki\u1ec3m tra ki\u1ec3u d\u1eef li\u1ec7u tr\u01b0\u1edbc, n\u1ebfu kh\u00f4ng s\u1ebd kh\u00f4ng th\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c thao t\u00e1c ti\u1ebfp theo.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">S\u1eed d\u1ee5ng &#8216;unknown&#8217; khuy\u1ebfn kh\u00edch t\u00ednh an to\u00e0n c\u1ee7a ki\u1ec3u t\u1ed1t h\u01a1n v\u00e0 gi\u00fap tr\u00e1nh l\u1ed7i runtime b\u1eb1ng c\u00e1ch bu\u1ed9c b\u1ea1n ph\u1ea3i th\u1ef1c hi\u1ec7n ki\u1ec3m tra ki\u1ec3u tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng bi\u1ebfn.<\/span><\/p>\n<h3><b>TypeScript gi\u00fap ph\u00e1t hi\u1ec7n l\u1ed7i s\u1edbm h\u01a1n so v\u1edbi JavaScript nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">TypeScript l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef c\u00f3 ki\u1ec3u t\u0129nh (statically typed), ngh\u0129a l\u00e0 n\u00f3 ki\u1ec3m tra l\u1ed7i ki\u1ec3u t\u1ea1i th\u1eddi \u0111i\u1ec3m bi\u00ean d\u1ecbch, thay v\u00ec \u0111\u1ec3 \u0111\u1ebfn runtime nh\u01b0 JavaScript. \u0110i\u1ec1u n\u00e0y cho ph\u00e9p ph\u00e1t hi\u1ec7n v\u00e0 s\u1eeda l\u1ed7i s\u1edbm trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n, gi\u1ea3m nguy c\u01a1 l\u1ed7i khi \u1ee9ng d\u1ee5ng ch\u1ea1y th\u1ef1c t\u1ebf.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Kh\u00f4ng kh\u1edbp ki\u1ec3u:<\/b><span style=\"font-weight: 400;\"> let num: number = &#8216;text&#8217;; (l\u1ed7i compile-time).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Thu\u1ed9c t\u00ednh kh\u00f4ng t\u1ed3n t\u1ea1i:<\/b><span style=\"font-weight: 400;\"> obj.name khi obj kh\u00f4ng c\u00f3 name.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>\u0110\u1ed1i s\u1ed1 sai: <\/b><span style=\"font-weight: 400;\">g\u1ecdi fn(123) khi h\u00e0m y\u00eau c\u1ea7u chu\u1ed7i.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Trong JavaScript, nh\u1eefng l\u1ed7i n\u00e0y ch\u1ec9 l\u1ed9 ra khi ch\u1ea1y m\u00e3, c\u00f2n TypeScript b\u00e1o l\u1ed7i ngay l\u1eadp t\u1ee9c.<\/span><\/p>\n<h3><b>B\u1ea1n hi\u1ec3u g\u00ec v\u1ec1 &#8216;type guards&#8217; trong TypeScript?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Type guards l\u00e0 c\u00e1c bi\u1ec3u th\u1ee9c trong TypeScript gi\u00fap ki\u1ec3m tra ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a m\u1ed9t bi\u1ebfn tr\u01b0\u1edbc khi s\u1eed d\u1ee5ng, gi\u00fap TypeScript hi\u1ec3u r\u00f5 ki\u1ec3u d\u1eef li\u1ec7u b\u00ean trong kh\u1ed1i ki\u1ec3m tra. Type guards s\u1ebd gi\u00fap vi\u1ebft m\u00e3 an to\u00e0n h\u01a1n v\u1ec1 ki\u1ec3u b\u1eb1ng c\u00e1ch thu h\u1eb9p ki\u1ec3u c\u1ee7a m\u1ed9t bi\u1ebfn trong m\u1ed9t kh\u1ed1i c\u00f3 \u0111i\u1ec1u ki\u1ec7n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Type guards ph\u1ed5 bi\u1ebfn bao g\u1ed3m c\u00e1c to\u00e1n t\u1eed &#8216;typeof&#8217; v\u00e0 &#8216;instanceof&#8217;, ki\u1ec3m tra ki\u1ec3u c\u1ee7a m\u1ed9t bi\u1ebfn v\u00e0 th\u1ec3 hi\u1ec7n c\u1ee7a m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng t\u01b0\u01a1ng \u1ee9ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function printValue(value: string | number) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0if (typeof value === \"string\") {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0console.log(value.toUpperCase());<\/span>\n<span style=\"font-weight: 400;\"> \u00a0} else {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0console.log(value.toFixed(2));<\/span>\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>Type assertion trong TypeScript l\u00e0 g\u00ec v\u00e0 khi n\u00e0o n\u00ean s\u1eed d\u1ee5ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Type assertion l\u00e0 m\u1ed9t c\u01a1 ch\u1ebf trong TypeScript cho ph\u00e9p b\u1ea1n ghi \u0111\u00e8 ki\u1ec3u suy ra v\u00e0 ch\u1ec9 \u0111\u1ecbnh m\u1ed9t ki\u1ec3u kh\u00e1c. T\u01b0\u01a1ng t\u1ef1 nh\u01b0 \u00e9p ki\u1ec3u trong c\u00e1c ng\u00f4n ng\u1eef kh\u00e1c nh\u01b0ng kh\u00f4ng th\u1ef1c hi\u1ec7n b\u1ea5t k\u1ef3 quy tr\u00ecnh ki\u1ec3m tra runtime ho\u1eb7c chuy\u1ec3n \u0111\u1ed5i n\u00e0o.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng type assertion khi b\u1ea1n ch\u1eafc ch\u1eafn v\u1ec1 ki\u1ec3u c\u1ee7a m\u1ed9t bi\u1ebfn trong m\u1ed9t ng\u1eef c\u1ea3nh c\u1ee5 th\u1ec3 v\u00e0 mu\u1ed1n th\u00f4ng b\u00e1o cho tr\u00ecnh bi\u00ean d\u1ecbch TypeScript v\u1ec1 \u0111i\u1ec1u \u0111\u00f3. V\u00ed d\u1ee5, khi x\u1eed l\u00fd c\u00e1c th\u01b0 vi\u1ec7n c\u1ee7a b\u00ean th\u1ee9 ba ho\u1eb7c ph\u00e2n t\u00edch c\u00fa ph\u00e1p JSON, kh\u1eb3ng \u0111\u1ecbnh ki\u1ec3u c\u00f3 th\u1ec3 h\u1eefu \u00edch.<\/span><\/p>\n<h3><b>Readonly trong TypeScript ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">&#8216;readonly&#8217; trong TypeScript \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 l\u00e0m cho c\u00e1c thu\u1ed9c t\u00ednh c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i. Khi m\u1ed9t thu\u1ed9c t\u00ednh \u0111\u01b0\u1ee3c \u0111\u00e1nh d\u1ea5u l\u00e0 &#8216;readonly&#8217;, n\u00f3 kh\u00f4ng th\u1ec3 \u0111\u01b0\u1ee3c g\u00e1n l\u1ea1i sau l\u1ea7n g\u00e1n ban \u0111\u1ea7u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y h\u1eefu \u00edch \u0111\u1ec3 t\u1ea1o c\u00e1c c\u1ea5u tr\u00fac d\u1eef li\u1ec7u kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i v\u00e0 t\u0103ng c\u01b0\u1eddng \u0111\u1ed9 tin c\u1eady c\u1ee7a m\u00e3 b\u1eb1ng c\u00e1ch ng\u0103n ch\u1eb7n c\u00e1c thay \u0111\u1ed5i kh\u00f4ng mong mu\u1ed1n.<\/span><\/p>\n<h3><b>TypeScript c\u00f3 h\u1ed7 tr\u1ee3 static class kh\u00f4ng? N\u1ebfu kh\u00f4ng, t\u1ea1i sao?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">TypeScript kh\u00f4ng h\u1ed7 tr\u1ee3 kh\u00e1i ni\u1ec7m &#8216;static class&#8217; (l\u1edbp m\u00e0 t\u1ea5t c\u1ea3 th\u00e0nh ph\u1ea7n \u0111\u1ec1u l\u00e0 static) nh\u01b0 trong C# ho\u1eb7c <\/span><a href=\"\/blog\/java-la-gi\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Java<\/span><\/a><span style=\"font-weight: 400;\">. Trong c\u00e1c ng\u00f4n ng\u1eef \u0111\u00f3, m\u1ecdi m\u00e3 ph\u1ea3i n\u1eb1m trong class, v\u00e0 static class gi\u00fap \u0111\u1ecbnh ngh\u0129a h\u00e0m\/d\u1eef li\u1ec7u kh\u00f4ng li\u00ean k\u1ebft v\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ng\u01b0\u1ee3c l\u1ea1i, TypeScript (c\u0169ng nh\u01b0 JavaScript) cho ph\u00e9p khai b\u00e1o h\u00e0m v\u00e0 d\u1eef li\u1ec7u \u0111\u1ed9c l\u1eadp m\u00e0 kh\u00f4ng c\u1ea7n class, n\u00ean kh\u00f4ng c\u1ea7n &#8216;static class&#8217;. Tuy nhi\u00ean, TypeScript h\u1ed7 tr\u1ee3 thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c static trong class.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class Example {\u00a0 <\/span>\n<span style=\"font-weight: 400;\"> \u00a0static greet() { return 'Hello'; }\u00a0\u00a0<\/span>\n<span style=\"font-weight: 400;\">}\u00a0 <\/span>\n<span style=\"font-weight: 400;\">console.log(Example.greet()); \/\/ Hello<\/span><\/pre>\n<h3><b>Type Assertion trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Type Assertion trong TypeScript cho ph\u00e9p b\u1ea1n n\u00f3i v\u1edbi tr\u00ecnh bi\u00ean d\u1ecbch r\u1eb1ng b\u1ea1n bi\u1ebft ch\u00ednh x\u00e1c ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a m\u1ed9t bi\u1ebfn t\u1ed1t h\u01a1n TypeScript suy lu\u1eadn. Th\u00f4ng th\u01b0\u1eddng, \u0111i\u1ec1u n\u00e0y x\u1ea3y ra khi b\u1ea1n bi\u1ebft ki\u1ec3u c\u1ee7a m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee5 th\u1ec3 h\u01a1n ki\u1ec3u hi\u1ec7n t\u1ea1i c\u1ee7a n\u00f3. Trong nh\u1eefng tr\u01b0\u1eddng h\u1ee3p nh\u01b0 v\u1eady, b\u1ea1n c\u00f3 th\u1ec3 y\u00eau c\u1ea7u tr\u00ecnh bi\u00ean d\u1ecbch TypeScript kh\u00f4ng suy ra ki\u1ec3u c\u1ee7a bi\u1ebfn b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c kh\u1eb3ng \u0111\u1ecbnh ki\u1ec3u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">TypeScript cung c\u1ea5p hai h\u00ecnh th\u1ee9c \u0111\u1ec3 kh\u1eb3ng \u0111\u1ecbnh c\u00e1c ki\u1ec3u:<\/span><\/p>\n<h4><b>c\u00fa ph\u00e1p as<\/b><\/h4>\n<pre><span style=\"font-weight: 400;\">let value: unknown = \"Foo\";<\/span>\n\n<span style=\"font-weight: 400;\">let len: number = (value as string).length;<\/span><\/pre>\n<h4><b>c\u00fa ph\u00e1p &lt;&gt;<\/b><\/h4>\n<pre><span style=\"font-weight: 400;\">let value: unknown = \"Foo\";<\/span>\n\n<span style=\"font-weight: 400;\">let len: number = (&lt;string&gt;value).length;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Type Assertion t\u01b0\u01a1ng t\u1ef1 nh\u01b0 c\u00e1c ki\u1ec3u trong c\u00e1c ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh kh\u00e1c nh\u01b0 C# ho\u1eb7c Java. Tuy nhi\u00ean, kh\u00f4ng gi\u1ed1ng nh\u01b0 c\u00e1c ng\u00f4n ng\u1eef \u0111\u00f3, kh\u00f4ng c\u00f3 h\u00ecnh ph\u1ea1t th\u1eddi gian ch\u1ea1y n\u00e0o \u0111\u1ed1i v\u1edbi vi\u1ec7c \u0111\u00f3ng h\u1ed9p v\u00e0 m\u1edf h\u1ed9p c\u00e1c bi\u1ebfn \u0111\u1ec3 ph\u00f9 h\u1ee3p v\u1edbi c\u00e1c ki\u1ec3u. Kh\u1eb3ng \u0111\u1ecbnh ki\u1ec3u ch\u1ec9 \u0111\u01a1n gi\u1ea3n cho tr\u00ecnh bi\u00ean d\u1ecbch TypeScript bi\u1ebft ki\u1ec3u c\u1ee7a bi\u1ebfn.<\/span><\/p>\n<h3><b>Type Alias trong TypeScript l\u00e0 g\u00ec? L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 t\u1ea1o Type Alias?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Type Alias cho ph\u00e9p b\u1ea1n \u0111\u1eb7t t\u00ean m\u1edbi cho m\u1ed9t ki\u1ec3u d\u1eef li\u1ec7u hi\u1ec7n c\u00f3, gi\u00fap m\u00e3 d\u1ec5 \u0111\u1ecdc h\u01a1n v\u00e0 tr\u00e1nh l\u1eb7p l\u1ea1i khi s\u1eed d\u1ee5ng nhi\u1ec1u l\u1ea7n. Ch\u00fang kh\u00f4ng t\u1ea1o ra ki\u1ec3u m\u1edbi m\u00e0 t\u1ea1o ra t\u00ean m\u1edbi tham chi\u1ebfu \u0111\u1ebfn ki\u1ec3u \u0111\u00f3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5, b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1eb7t t\u00ean m\u1edbi cho m\u1ed9t ki\u1ec3u h\u1ee3p nh\u1ea5t \u0111\u1ec3 tr\u00e1nh nh\u1eadp t\u1ea5t c\u1ea3 c\u00e1c ki\u1ec3u \u1edf m\u1ecdi n\u01a1i m\u00e0 gi\u00e1 tr\u1ecb \u0111ang \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">type alphanumeric = string | number;<\/span>\n\n<span style=\"font-weight: 400;\">let value: alphanumeric = \"\";<\/span>\n\n<span style=\"font-weight: 400;\">value = 10;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Kh\u00e1c v\u1edbi interface, type alias kh\u00f4ng th\u1ec3 m\u1edf r\u1ed9ng b\u1eb1ng extends nh\u01b0ng linh ho\u1ea1t h\u01a1n v\u1edbi union\/intersection.<\/span><\/p>\n<p><b>L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 bi\u1ebfn thu\u1ed9c t\u00ednh c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng th\u00e0nh b\u1ea5t bi\u1ebfn (immutable) trong TypeScript?<\/b><\/p>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u00f3 th\u1ec3 \u0111\u00e1nh d\u1ea5u c\u00e1c thu\u1ed9c t\u00ednh \u0111\u1ed1i t\u01b0\u1ee3ng l\u00e0 kh\u00f4ng thay \u0111\u1ed5i b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a readonly tr\u01b0\u1edbc t\u00ean thu\u1ed9c t\u00ednh. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">interface Coordinate {<\/span>\n\n<span style=\"font-weight: 400;\">readonly x: number;<\/span>\n\n<span style=\"font-weight: 400;\">readonly y: number;<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Khi b\u1ea1n \u0111\u00e1nh d\u1ea5u m\u1ed9t thu\u1ed9c t\u00ednh l\u00e0 ch\u1ec9 \u0111\u1ecdc, n\u00f3 ch\u1ec9 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c thi\u1ebft l\u1eadp khi b\u1ea1n kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng. Sau khi \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c t\u1ea1o, b\u1ea1n kh\u00f4ng th\u1ec3 thay \u0111\u1ed5i n\u00f3.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">let c: Coordinate = { x: 5, y: 15 };<\/span>\n\n<span style=\"font-weight: 400;\">c.x = 20; \/\/ Cannot assign to 'x' because it is a read-only property.(2540)<\/span><\/pre>\n<h3><b>M\u1ee5c \u0111\u00edch c\u1ee7a to\u00e1n t\u1eed \u2018in\u2019 trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">To\u00e1n t\u1eed \u201cin\u201d \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 t\u00ecm xem m\u1ed9t thu\u1ed9c t\u00ednh c\u00f3 n\u1eb1m trong \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh hay kh\u00f4ng. \u201cin\u201d s\u1ebd tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb true n\u1ebfu thu\u1ed9c t\u00ednh \u0111\u00f3 thu\u1ed9c v\u1ec1 \u0111\u1ed1i t\u01b0\u1ee3ng. N\u1ebfu kh\u00f4ng, s\u1ebd tr\u1ea3 v\u1ec1 false.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">const car = { make: 'Mercedes', model: 'S450', year: 2023 };<\/span>\n\n<span style=\"font-weight: 400;\">console.log('model' in car);\u00a0 \/\/ true<\/span>\n\n<span style=\"font-weight: 400;\">console.log('test' in car);\u00a0 \/\/ false<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">N\u00f3 c\u0169ng ho\u1ea1t \u0111\u1ed9ng v\u1edbi thu\u1ed9c t\u00ednh k\u1ebf th\u1eeba:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class Car { year = 2023; }\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400;\">const c = new Car();\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400;\">console.log('year' in c); \/\/ true<\/span><\/pre>\n<h3><b>Kh\u00e1i ni\u1ec7m k\u1ebf th\u1eeba (inheritance) trong TypeScript l\u00e0 g\u00ec?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">K\u1ebf th\u1eeba (inheritance) cho ph\u00e9p b\u1ea1n t\u1ea1o ra m\u1ed9t class m\u1edbi d\u1ef1a tr\u00ean class hi\u1ec7n c\u00f3 b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a \u201cextends\u201d. L\u1edbp k\u1ebf th\u1eeba m\u1ed9t l\u1edbp kh\u00e1c \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 l\u1edbp d\u1eabn xu\u1ea5t (the derived class) v\u00e0 l\u1edbp \u0111\u01b0\u1ee3c k\u1ebf th\u1eeba \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 l\u1edbp c\u01a1 s\u1edf (the base class).<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Trong TypeScript, m\u1ed9t l\u1edbp ch\u1ec9 c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng m\u1ed9t l\u1edbp. TypeScript s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a \u2018extends\u2019 \u0111\u1ec3 ch\u1ec9 \u0111\u1ecbnh m\u1ed1i quan h\u1ec7 gi\u1eefa l\u1edbp c\u01a1 s\u1edf v\u00e0 c\u00e1c l\u1edbp d\u1eabn xu\u1ea5t.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class Rectangle {<\/span>\n<span style=\"font-weight: 400;\">length: number;<\/span>\n<span style=\"font-weight: 400;\">breadth: number<\/span>\n\n<span style=\"font-weight: 400;\">constructor(length: number, breadth: number) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0this.length = length;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0this.breadth = breadth<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">area(): number {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0return this.length * this.breadth;<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">class Square extends Rectangle {<\/span>\n<span style=\"font-weight: 400;\">constructor(side: number) {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0super(side, side);<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">volume() {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0return \"Square doesn't have a volume!\"<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">const sq = new Square(10);<\/span>\n\n<span style=\"font-weight: 400;\">console.log(sq.area());\u00a0 \/\/ 100<\/span>\n<span style=\"font-weight: 400;\">console.log(sq.volume());\u00a0 \/\/ \"Square doesn't have a volume!\"<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u1ede v\u00ed d\u1ee5 tr\u00ean, v\u00ec Square class m\u1edf r\u1ed9ng ch\u1ee9c n\u0103ng t\u1eeb Rectangle class n\u00ean c\u00f3 th\u1ec3 t\u1ea1o instance c\u1ee7a square v\u00e0 g\u1ecdi c\u1ea3 hai b\u1eb1ng ph\u01b0\u01a1ng th\u1ee9c area() v\u00e0 volume().<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_TypeScript_ve_type_inference\"><\/span><b> C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript v\u1ec1 type inference\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>Type inference ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o trong TypeScript v\u00e0 l\u1ee3i \u00edch c\u1ee7a ch\u00fang?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Type inference trong TypeScript l\u00e0 qu\u00e1 tr\u00ecnh m\u00e0 TypeScript t\u1ef1 \u0111\u1ed9ng suy lu\u1eadn ki\u1ec3u d\u1eef li\u1ec7u c\u1ee7a bi\u1ebfn ho\u1eb7c h\u00e0m d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c g\u00e1n ban \u0111\u1ea7u m\u00e0 kh\u00f4ng c\u1ea7n khai b\u00e1o ki\u1ec3u r\u00f5 r\u00e0ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Nh\u1eefng l\u1ee3i \u00edch c\u1ee7a Type inference bao g\u1ed3m:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u00fap m\u00e3 ng\u1eafn g\u1ecdn, d\u1ec5 \u0111\u1ecdc h\u01a1n m\u00e0 v\u1eabn \u0111\u1ea3m b\u1ea3o an to\u00e0n ki\u1ec3u d\u1eef li\u1ec7u.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Gi\u00fap t\u0103ng hi\u1ec7u su\u1ea5t l\u00e0m vi\u1ec7c khi kh\u00f4ng ph\u1ea3i khai b\u00e1o ki\u1ec3u d\u1eef li\u1ec7u th\u1ee7 c\u00f4ng cho m\u1ecdi bi\u1ebfn.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Duy tr\u00ec t\u00ednh an to\u00e0n c\u1ee7a ki\u1ec3u trong khi cho ph\u00e9p phong c\u00e1ch m\u00e3 h\u00f3a gi\u1ed1ng JavaScript h\u01a1n.\u00a0<\/span><\/li>\n<\/ul>\n<h3><b>Gi\u1ea3i th\u00edch c\u00e1ch type inference ho\u1ea1t \u0111\u1ed9ng v\u1edbi khai b\u00e1o const trong TypeScript?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi s\u1eed d\u1ee5ng const \u0111\u1ec3 khai b\u00e1o m\u1ed9t bi\u1ebfn trong TypeScript, type inference s\u1ebd l\u00e0 h\u1eb1ng s\u1ed1 kh\u00f4ng thay \u0111\u1ed5i. Thay v\u00ec suy lu\u1eadn m\u1ed9t ki\u1ec3u chung, n\u00f3 suy lu\u1eadn m\u1ed9t ki\u1ec3u theo ngh\u0129a \u0111en d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c g\u00e1n.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">const x = &#8216;hello&#8217;<\/span><span style=\"font-weight: 400;\"> s\u1ebd suy lu\u1eadn ki\u1ec3u l\u00e0 <\/span><span style=\"font-weight: 400;\">&#8216;hello&#8217;<\/span><span style=\"font-weight: 400;\"> theo ngh\u0129a \u0111en thay v\u00ec ch\u1ec9 l\u00e0 chu\u1ed7i (string).<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">const arr = [1, 2, 3]<\/span><span style=\"font-weight: 400;\"> s\u1ebd suy lu\u1eadn l\u00e0 <\/span><span style=\"font-weight: 400;\">number[]<\/span><span style=\"font-weight: 400;\">, nh\u01b0ng \u0111\u1ed9 d\u00e0i c\u0169ng \u0111\u01b0\u1ee3c coi l\u00e0 m\u1ed9t ph\u1ea7n c\u1ee7a ki\u1ec3u.<\/span><\/li>\n<\/ul>\n<h3><b>TypeScript suy lu\u1eadn ki\u1ec3u tr\u1ea3 v\u1ec1 cho h\u00e0m nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">TypeScript suy ra c\u00e1c ki\u1ec3u tr\u1ea3 v\u1ec1 c\u1ee7a h\u00e0m d\u1ef1a tr\u00ean gi\u00e1 tr\u1ecb \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 trong h\u00e0m. Ngh\u0129a l\u00e0 s\u1ebd ki\u1ec3m tra t\u1ea5t c\u1ea3 c\u00e1c c\u00e2u l\u1ec7nh tr\u1ea3 v\u1ec1 trong h\u00e0m v\u00e0 x\u00e1c \u0111\u1ecbnh ki\u1ec3u ph\u00f9 h\u1ee3p nh\u1ea5t bao g\u1ed3m t\u1ea5t c\u1ea3 c\u00e1c gi\u00e1 tr\u1ecb tr\u1ea3 v\u1ec1 c\u00f3 th\u1ec3.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu m\u1ed9t h\u00e0m lu\u00f4n tr\u1ea3 v\u1ec1 s\u1ed1, TypeScript s\u1ebd suy ra ki\u1ec3u tr\u1ea3 v\u1ec1 l\u00e0 s\u1ed1.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">N\u1ebfu m\u1ed9t h\u00e0m c\u00f3 th\u1ec3 tr\u1ea3 v\u1ec1 chu\u1ed7i ho\u1eb7c null, TypeScript s\u1ebd suy ra ki\u1ec3u h\u1ee3p nh\u1ea5t string | null.<\/span><\/li>\n<\/ul>\n<h3><b>Contextual typing trong TypeScript l\u00e0 g\u00ec v\u00e0 n\u00f3 li\u00ean quan g\u00ec \u0111\u1ebfn type inference?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Contextual typing l\u00e0 m\u1ed9t d\u1ea1ng suy lu\u1eadn ki\u1ec3u trong TypeScript, trong \u0111\u00f3 TypeScript s\u1ebd suy lu\u1eadn ki\u1ec3u d\u1ef1a tr\u00ean ng\u1eef c\u1ea3nh s\u1eed d\u1ee5ng bi\u1ebfn ho\u1eb7c h\u00e0m. \u0110i\u1ec1u n\u00e0y th\u01b0\u1eddng x\u1ea3y ra v\u1edbi c\u00e1c \u0111\u1ed1i s\u1ed1 h\u00e0m, trong \u0111\u00f3 ki\u1ec3u mong \u0111\u1ee3i \u0111\u01b0\u1ee3c bi\u1ebft t\u1eeb khai b\u00e1o c\u1ee7a h\u00e0m.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5, trong tr\u00ecnh x\u1eed l\u00fd s\u1ef1 ki\u1ec7n ho\u1eb7c h\u00e0m g\u1ecdi l\u1ea1i (callback function), TypeScript c\u00f3 th\u1ec3 suy ra ki\u1ec3u c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng s\u1ef1 ki\u1ec7n ho\u1eb7c tham s\u1ed1 g\u1ecdi l\u1ea1i d\u1ef1a tr\u00ean c\u00e1ch ch\u00fang \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng. \u0110i\u1ec1u n\u00e0y cho ph\u00e9p ki\u1ec3m tra ki\u1ec3u t\u1ed1t h\u01a1n m\u00e0 kh\u00f4ng c\u1ea7n ch\u00fa th\u00edch r\u00f5 r\u00e0ng.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, b\u1ea1n c\u00f3 th\u1ec3 \u0111\u01b0a th\u00eam v\u00ed d\u1ee5 v\u1ec1 tr\u01b0\u1eddng h\u1ee3p s\u1eed d\u1ee5ng contextual typing theo ng\u1eef c\u1ea3nh, ch\u1eb3ng h\u1ea1n nh\u01b0 trong c\u00e1c ph\u01b0\u01a1ng th\u1ee9c m\u1ea3ng nh\u01b0 map ho\u1eb7c filter.\u00a0<\/span><\/p>\n<h3><b>Type widening ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o trong TypeScript v\u00e0 khi n\u00e0o n\u00f3 x\u1ea3y ra?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Type widening l\u00e0 m\u1ed9t qu\u00e1 tr\u00ecnh TypeScript suy lu\u1eadn ki\u1ec3u d\u1eef li\u1ec7u r\u1ed9ng h\u01a1n khi kh\u00f4ng c\u00f3 r\u00e0ng bu\u1ed9c c\u1ee5 th\u1ec3. \u0110i\u1ec1u n\u00e0y th\u01b0\u1eddng x\u1ea3y ra v\u1edbi c\u00e1c khai b\u00e1o let khi gi\u00e1 tr\u1ecb ban \u0111\u1ea7u kh\u00f4ng bi\u1ec3u di\u1ec5n \u0111\u1ea7y \u0111\u1ee7 t\u1ea5t c\u1ea3 c\u00e1c gi\u00e1 tr\u1ecb c\u00f3 th\u1ec3 c\u00f3 trong t\u01b0\u01a1ng lai.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">let x = 3<\/span><span style=\"font-weight: 400;\"> suy ra x l\u00e0 s\u1ed1, kh\u00f4ng ph\u1ea3i l\u00e0 s\u1ed1 3 theo ngh\u0129a \u0111en<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">let y = null<\/span><span style=\"font-weight: 400;\"> suy ra y l\u00e0 any, kh\u00f4ng ph\u1ea3i l\u00e0 null. N\u1ebfu t\u1eaft strictNullChecks, n\u00f3 v\u1eabn l\u00e0 null, kh\u00f4ng ph\u1ea3i any.<\/span><\/li>\n<\/ul>\n<h3><b>TypeScript suy lu\u1eadn ki\u1ec3u trong array v\u00e0 object destructuring nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong m\u1ea3ng v\u00e0 object destructuring, TypeScript suy ra c\u00e1c ki\u1ec3u d\u1ef1a tr\u00ean c\u1ea5u tr\u00fac v\u00e0 ki\u1ec3u c\u1ee7a m\u1ea3ng ho\u1eb7c \u0111\u1ed1i t\u01b0\u1ee3ng.\u00a0<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ed1i v\u1edbi m\u1ea3ng, TypeScript suy lu\u1eadn ki\u1ec3u d\u1ef1a tr\u00ean th\u1ee9 t\u1ef1 ph\u1ea7n t\u1eed.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">\u0110\u1ed1i v\u1edbi object, TypeScript suy lu\u1eadn ki\u1ec3u d\u1ef1a tr\u00ean t\u00ean thu\u1ed9c t\u00ednh.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Trong <\/span><span style=\"font-weight: 400;\">const [first, second] = [1, &#8216;two&#8217;]<\/span><span style=\"font-weight: 400;\">, TypeScript suy ra first l\u00e0 s\u1ed1 v\u00e0 second l\u00e0 chu\u1ed7i.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Trong <\/span><span style=\"font-weight: 400;\">const {name, age} = person<\/span><span style=\"font-weight: 400;\">, c\u00e1c ki\u1ec3u \u0111\u01b0\u1ee3c suy ra d\u1ef1a tr\u00ean c\u00e1c thu\u1ed9c t\u00ednh c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng person.\u00a0<\/span><\/li>\n<\/ul>\n<h3><b>TypeScript x\u1eed l\u00fd type inference trong generic function nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Trong generics, TypeScript suy lu\u1eadn ki\u1ec3u d\u1ef1a tr\u00ean tham s\u1ed1 truy\u1ec1n v\u00e0o khi g\u1ecdi h\u00e0m. \u0110i\u1ec1u n\u00e0y cho ph\u00e9p m\u00e3 linh ho\u1ea1t v\u00e0 c\u00f3 th\u1ec3 t\u00e1i s\u1eed d\u1ee5ng trong khi v\u1eabn duy tr\u00ec \u0111\u01b0\u1ee3c t\u00ednh an to\u00e0n c\u1ee7a ki\u1ec3u.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V\u00ed d\u1ee5, trong m\u1ed9t h\u00e0m:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function identity&lt;T&gt;(arg: T): T<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">TypeScript s\u1ebd suy ra ki\u1ec3u T d\u1ef1a tr\u00ean \u0111\u1ed1i s\u1ed1 \u0111\u01b0\u1ee3c truy\u1ec1n v\u00e0o. N\u1ebfu b\u1ea1n g\u1ecdi <\/span><span style=\"font-weight: 400;\">identity(42)<\/span><span style=\"font-weight: 400;\">, T \u0111\u01b0\u1ee3c suy ra l\u00e0 s\u1ed1.<\/span><\/p>\n<h3><b>noImplicitAny trong TypeScript l\u00e0 g\u00ec v\u00e0 \u1ea3nh h\u01b0\u1edfng nh\u01b0 th\u1ebf n\u00e0o \u0111\u1ebfn type inference?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">T\u00f9y ch\u1ecdn tr\u00ecnh bi\u00ean d\u1ecbch &#8216;noImplicitAny&#8217; trong TypeScript ng\u0103n tr\u00ecnh bi\u00ean d\u1ecbch suy ra ki\u1ec3u any khi kh\u00f4ng th\u1ec3 x\u00e1c \u0111\u1ecbnh ki\u1ec3u c\u1ee5 th\u1ec3 h\u01a1n. Khi t\u00f9y ch\u1ecdn n\u00e0y \u0111\u01b0\u1ee3c b\u1eadt, TypeScript s\u1ebd \u0111\u01b0a ra l\u1ed7i thay v\u00ec s\u1eed d\u1ee5ng any m\u1ed9t c\u00e1ch \u00e2m th\u1ea7m.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">function log(x) { console.log(x); } \/\/ L\u1ed7i v\u1edbi noImplicitAny: x implicitly has an 'any' type\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400;\">function log(x: number) { console.log(x); } \/\/ OK\u00a0<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">T\u00f9y ch\u1ecdn n\u00e0y khuy\u1ebfn kh\u00edch nh\u1eadp r\u00f5 r\u00e0ng h\u01a1n v\u00e0 c\u00f3 th\u1ec3 gi\u00fap ph\u00e1t hi\u1ec7n s\u1edbm c\u00e1c s\u1ef1 c\u1ed1 li\u00ean quan \u0111\u1ebfn ki\u1ec3u ti\u1ec1m \u1ea9n. \u0110i\u1ec1u n\u00e0y \u0111\u1eb7c bi\u1ec7t h\u1eefu \u00edch trong c\u00e1c t\u00ecnh hu\u1ed1ng m\u00e0 suy lu\u1eadn ki\u1ec3u qu\u00e1 d\u1ec5 d\u00e0ng, ch\u1eb3ng h\u1ea1n nh\u01b0 v\u1edbi c\u00e1c tham s\u1ed1 h\u00e0m kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cau_hoi_phong_van_TypeScript_xu_ly_tinh_huong\"><\/span><b>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>B\u1ea1n s\u1ebd x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng ph\u1ea3i l\u00e0m vi\u1ec7c v\u1edbi object ho\u1eb7c bi\u1ebfn c\u00f3 th\u1ec3 null trong TypeScript nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi l\u00e0m vi\u1ec7c v\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng ho\u1eb7c bi\u1ebfn c\u00f3 th\u1ec3 null trong TypeScript, tr\u01b0\u1edbc ti\u00ean, t\u00f4i s\u1ebd s\u1eed d\u1ee5ng to\u00e1n t\u1eed Optional Chaining (?.) \u0111\u1ec3 ki\u1ec3m tra s\u1ef1 t\u1ed3n t\u1ea1i c\u1ee7a thu\u1ed9c t\u00ednh tr\u01b0\u1edbc khi truy c\u1eadp, nh\u1eb1m tr\u00e1nh l\u1ed7i TypeError khi truy c\u1eadp v\u00e0o thu\u1ed9c t\u00ednh c\u1ee7a null ho\u1eb7c undefined. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">const value = obj?.property?.subProperty;<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, t\u00f4i c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng to\u00e1n t\u1eed Nullish Coalescing (??) \u0111\u1ec3 cung c\u1ea5p gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh n\u1ebfu bi\u1ebfn c\u00f3 gi\u00e1 tr\u1ecb null ho\u1eb7c undefined, \u0111\u1ea3m b\u1ea3o \u1ee9ng d\u1ee5ng ho\u1ea1t \u0111\u1ed9ng \u1ed5n \u0111\u1ecbnh:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">const name = user.name ?? 'Unknown';<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u0103ng c\u01b0\u1eddng ki\u1ec3m tra ki\u1ec3u, c\u00f3 th\u1ec3 b\u1eadt strictNullChecks trong tsconfig.json, gi\u00fap TypeScript ph\u00e1t hi\u1ec7n c\u00e1c l\u1ed7i li\u00ean quan \u0111\u1ebfn null v\u00e0 undefined ngay trong qu\u00e1 tr\u00ecnh bi\u00ean d\u1ecbch.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, b\u1ea1n n\u00ean gi\u1ea3i th\u00edch c\u00e1ch s\u1eed d\u1ee5ng Optional Chaining v\u00e0 Nullish Coalescing \u0111\u1ec3 th\u1ec3 hi\u1ec7n kh\u1ea3 n\u0103ng ki\u1ec3m so\u00e1t d\u1eef li\u1ec7u nullable trong TypeScript m\u1ed9t c\u00e1ch an to\u00e0n v\u00e0 hi\u1ec7u qu\u1ea3. \u0110\u1ed3ng th\u1eddi, vi\u1ec7c \u0111\u1ec1 c\u1eadp \u0111\u1ebfn c\u1edd strictNullChecks cho th\u1ea5y b\u1ea1n hi\u1ec3u s\u00e2u v\u1ec1 c\u00e1ch TypeScript x\u1eed l\u00fd c\u00e1c ki\u1ec3u nullable.<\/span><\/p><\/blockquote>\n<h3><b>B\u1ea1n s\u1ebd s\u1eed d\u1ee5ng chi\u1ebfn l\u01b0\u1ee3c n\u00e0o \u0111\u1ec3 m\u1edf r\u1ed9ng v\u00e0 t\u00e1i s\u1eed d\u1ee5ng interfaces \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o t\u00ednh linh ho\u1ea1t trong d\u1ef1 \u00e1n TypeScript?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 m\u1edf r\u1ed9ng v\u00e0 t\u00e1i s\u1eed d\u1ee5ng interface trong TypeScript, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng k\u1ef9 thu\u1eadt k\u1ebf th\u1eeba b\u1eb1ng t\u1eeb kh\u00f3a extends, gi\u00fap chia s\u1ebb thu\u1ed9c t\u00ednh chung gi\u1eefa c\u00e1c interface v\u00e0 tr\u00e1nh l\u1eb7p l\u1ea1i m\u00e3 ngu\u1ed3n. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">interface Person {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0name: string;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0age: number;<\/span>\n<span style=\"font-weight: 400;\">}<\/span>\n\n<span style=\"font-weight: 400;\">interface Employee extends Person {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0employeeId: string;<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Interface Merging khi l\u00e0m vi\u1ec7c v\u1edbi c\u00e1c th\u01b0 vi\u1ec7n b\u00ean th\u1ee9 ba, gi\u00fap m\u1edf r\u1ed9ng ki\u1ec3u c\u1ee7a ch\u00fang m\u1ed9t c\u00e1ch linh ho\u1ea1t m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i ch\u1ec9nh s\u1eeda tr\u1ef1c ti\u1ebfp m\u00e3 ngu\u1ed3n c\u1ee7a th\u01b0 vi\u1ec7n.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">interface Lib { foo(): void; }\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400;\">interface Lib { bar(): void; } \/\/ G\u1ed9p th\u00e0nh { foo(): void; bar(): void; }<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">Ho\u1eb7c \u0111\u1ec3 t\u0103ng t\u00ednh linh ho\u1ea1t, c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Generics khi \u0111\u1ecbnh ngh\u0129a interface, cho ph\u00e9p t\u00e1i s\u1eed d\u1ee5ng interface cho nhi\u1ec1u lo\u1ea1i d\u1eef li\u1ec7u kh\u00e1c nhau m\u00e0 kh\u00f4ng c\u1ea7n ph\u1ea3i \u0111\u1ecbnh ngh\u0129a l\u1ea1i.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">interface Container&lt;T&gt; { value: T; }\u00a0\u00a0<\/span>\n\n<span style=\"font-weight: 400;\">const numContainer: Container&lt;number&gt; = { value: 42 };\u00a0<\/span><\/pre>\n<h3><b>B\u1ea1n c\u00f3 th\u1ec3 gi\u1ea3i th\u00edch c\u00e1ch thi\u1ebft k\u1ebf h\u1ec7 th\u1ed1ng ph\u1ee9c t\u1ea1p b\u1eb1ng TypeScript \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o an to\u00e0n ki\u1ec3u d\u1eef li\u1ec7u kh\u00f4ng?<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Khi thi\u1ebft k\u1ebf m\u1ed9t h\u1ec7 th\u1ed1ng ph\u1ee9c t\u1ea1p b\u1eb1ng TypeScript, tr\u01b0\u1edbc ti\u00ean t\u00f4i s\u1ebd x\u00e2y d\u1ef1ng c\u00e1c m\u00f4 h\u00ecnh d\u1eef li\u1ec7u b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng interface v\u00e0 type alias \u0111\u1ec3 \u0111\u1ea3m b\u1ea3o an to\u00e0n ki\u1ec3u d\u1eef li\u1ec7u v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec. T\u00f4i s\u1ebd chia nh\u1ecf m\u00f4 h\u00ecnh th\u00e0nh c\u00e1c ph\u1ea7n c\u00f3 th\u1ec3 t\u00e1i s\u1eed d\u1ee5ng v\u00e0 m\u1edf r\u1ed9ng \u0111\u01b0\u1ee3c.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T\u00f4i c\u0169ng s\u1ebd s\u1eed d\u1ee5ng Generics \u0111\u1ec3 t\u1ea1o ra c\u00e1c h\u00e0m v\u00e0 class linh ho\u1ea1t nh\u01b0ng v\u1eabn \u0111\u1ea3m b\u1ea3o ki\u1ec3m tra ki\u1ec3u ch\u1eb7t ch\u1ebd. V\u00ed d\u1ee5:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class DataService&lt;T&gt; {<\/span>\n<span style=\"font-weight: 400;\">\u00a0\u00a0constructor(private data: T[]) {}<\/span>\n\n<span style=\"font-weight: 400;\"> \u00a0getAll(): T[] {<\/span>\n<span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0return this.data;<\/span>\n<span style=\"font-weight: 400;\"> \u00a0}<\/span>\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">\u0110\u1ec3 t\u0103ng c\u01b0\u1eddng an to\u00e0n ki\u1ec3u d\u1eef li\u1ec7u, t\u00f4i s\u1ebd b\u1eadt c\u00e1c t\u00f9y ch\u1ecdn ki\u1ec3m tra ch\u1eb7t ch\u1ebd trong tsconfig.json, nh\u01b0 strictNullChecks hay noImplicitAny, gi\u00fap ph\u00e1t hi\u1ec7n l\u1ed7i t\u1eeb s\u1edbm trong qu\u00e1 tr\u00ecnh bi\u00ean d\u1ecbch.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Ngo\u00e0i ra, t\u00f4i s\u1ebd \u00e1p d\u1ee5ng c\u00e1c Design Pattern nh\u01b0 Dependency Injection k\u1ebft h\u1ee3p v\u1edbi TypeScript Decorators \u0111\u1ec3 qu\u1ea3n l\u00fd ph\u1ee5 thu\u1ed9c v\u00e0 m\u1edf r\u1ed9ng t\u00ednh n\u0103ng linh ho\u1ea1t h\u01a1n.<\/span><\/p>\n<blockquote><p><span style=\"font-weight: 400;\">B\u1ea1n n\u00ean tr\u00ecnh b\u00e0y chi ti\u1ebft c\u00e1ch t\u1ed5 ch\u1ee9c m\u00f4 h\u00ecnh d\u1eef li\u1ec7u, s\u1eed d\u1ee5ng Generics v\u00e0 \u00e1p d\u1ee5ng c\u00e1c Design Pattern ph\u00f9 h\u1ee3p \u0111\u1ec3 th\u1ec3 hi\u1ec7n kh\u1ea3 n\u0103ng thi\u1ebft k\u1ebf h\u1ec7 th\u1ed1ng ph\u1ee9c t\u1ea1p nh\u01b0ng v\u1eabn \u0111\u1ea3m b\u1ea3o an to\u00e0n ki\u1ec3u d\u1eef li\u1ec7u trong TypeScript.<\/span><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket_ve_cau_hoi_phong_van_TypeScript\"><\/span><b>T\u1ed5ng k\u1ebft v\u1ec1 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript\u00a0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><b>C\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript<\/b><span style=\"font-weight: 400;\"> c\u00f3 th\u1ec3 bao g\u1ed3m t\u1eeb ki\u1ebfn th\u1ee9c c\u01a1 b\u1ea3n v\u1ec1 c\u00fa ph\u00e1p \u0111\u1ebfn c\u00e1c kh\u00e1i ni\u1ec7m v\u1ec1 type, inference hay c\u00e1c c\u00e2u h\u1ecfi x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng. Vi\u1ec7c chu\u1ea9n b\u1ecb k\u1ef9 c\u00e0ng s\u1ebd gi\u00fap b\u1ea1n t\u1ef1 tin v\u00e0 t\u1ea1o \u1ea5n t\u01b0\u1ee3ng m\u1ea1nh m\u1ebd tr\u01b0\u1edbc nh\u00e0 tuy\u1ec3n d\u1ee5ng. H\u00e3y th\u01b0\u1eddng xuy\u00ean c\u1eadp nh\u1eadt ki\u1ebfn th\u1ee9c m\u1edbi \u0111\u1ec3 c\u00f3 s\u1ef1 chu\u1ea9n b\u1ecb t\u1ed1t nh\u1ea5t trong bu\u1ed5i ph\u1ecfng v\u1ea5n s\u1eafp t\u1edbi nh\u00e9. Ch\u00fac b\u1ea1n th\u00e0nh c\u00f4ng!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0110\u1ec3 s\u1ef1 chu\u1ea9n b\u1ecb t\u1ed1t cho bu\u1ed5i ph\u1ecfng v\u1ea5n, b\u1ea1n c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c v\u1ec1 TypeScript t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao. B\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript sau \u0111\u00e2y bao g\u1ed3m c\u00e1c c\u00e2u h\u1ecfi v\u1ec1 c\u00fa ph\u00e1p, th\u1ef1c h\u00e0nh m\u00e3, gi\u1ea3i \u0111\u00e1p v\u1ec1 type safety hay c\u00e1ch x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng khi d\u00f9ng [&hellip;]<\/p>\n","protected":false},"author":207,"featured_media":85305,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109,105],"tags":[],"class_list":["post-85275","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it","category-phong-van-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn - ITviec Blog<\/title>\n<meta name=\"description\" content=\"B\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn v\u00e0 c\u00e1ch tr\u1ea3 l\u1eddi chi ti\u1ebft gi\u00fap b\u1ea1n t\u1ef1 tin chinh ph\u1ee5c m\u1ecdi v\u00f2ng ph\u1ecfng v\u1ea5n v\u1ec1 l\u1eadp tr\u00ecnh TypeScript.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn\" \/>\n<meta property=\"og:description\" content=\"\u0110\u1ec3 s\u1ef1 chu\u1ea9n b\u1ecb t\u1ed1t cho bu\u1ed5i ph\u1ecfng v\u1ea5n, b\u1ea1n c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c v\u1ec1 TypeScript t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao. B\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript sau \u0111\u00e2y bao g\u1ed3m\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-26T08:11:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-17T08:27:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/cau-hoi-phong-van-typescript-vippro-scaled.png\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"337\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Uyen Ngo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"Uyen Ngo\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"34 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn - ITviec Blog","description":"B\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn v\u00e0 c\u00e1ch tr\u1ea3 l\u1eddi chi ti\u1ebft gi\u00fap b\u1ea1n t\u1ef1 tin chinh ph\u1ee5c m\u1ecdi v\u00f2ng ph\u1ecfng v\u1ea5n v\u1ec1 l\u1eadp tr\u00ecnh TypeScript.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/","og_locale":"vi_VN","og_type":"article","og_title":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn","og_description":"\u0110\u1ec3 s\u1ef1 chu\u1ea9n b\u1ecb t\u1ed1t cho bu\u1ed5i ph\u1ecfng v\u1ea5n, b\u1ea1n c\u1ea7n n\u1eafm v\u1eefng ki\u1ebfn th\u1ee9c v\u1ec1 TypeScript t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn n\u00e2ng cao. B\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript sau \u0111\u00e2y bao g\u1ed3m","og_url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2025-03-26T08:11:36+00:00","article_modified_time":"2025-06-17T08:27:08+00:00","og_image":[{"width":640,"height":337,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/cau-hoi-phong-van-typescript-vippro-scaled.png","type":"image\/png"}],"author":"Uyen Ngo","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Uyen Ngo","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"34 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/"},"author":{"name":"Uyen Ngo","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/f4cd1226846e0258c664e170d3e52d20"},"headline":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn","datePublished":"2025-03-26T08:11:36+00:00","dateModified":"2025-06-17T08:27:08+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/"},"wordCount":9895,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/cau-hoi-phong-van-typescript-vippro-scaled.png","articleSection":["Chuy\u00ean m\u00f4n IT","Ph\u1ecfng v\u1ea5n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/","url":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/","name":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/cau-hoi-phong-van-typescript-vippro-scaled.png","datePublished":"2025-03-26T08:11:36+00:00","dateModified":"2025-06-17T08:27:08+00:00","description":"B\u1ed9 c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn v\u00e0 c\u00e1ch tr\u1ea3 l\u1eddi chi ti\u1ebft gi\u00fap b\u1ea1n t\u1ef1 tin chinh ph\u1ee5c m\u1ecdi v\u00f2ng ph\u1ecfng v\u1ea5n v\u1ec1 l\u1eadp tr\u00ecnh TypeScript.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/cau-hoi-phong-van-typescript-vippro-scaled.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2025\/03\/cau-hoi-phong-van-typescript-vippro-scaled.png","width":640,"height":337,"caption":"c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n typesript - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/cau-hoi-phong-van-typescript-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u1ee8ng tuy\u1ec3n &amp; Th\u0103ng ti\u1ebfn","item":"https:\/\/itviec.com\/blog\/ung-tuyen-va-thang-tien\/"},{"@type":"ListItem","position":2,"name":"Ph\u1ecfng v\u1ea5n IT","item":"https:\/\/itviec.com\/blog\/ung-tuyen-va-thang-tien\/phong-van-it\/"},{"@type":"ListItem","position":3,"name":"Top 30+ c\u00e2u h\u1ecfi ph\u1ecfng v\u1ea5n TypeScript ph\u1ed5 bi\u1ebfn"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/f4cd1226846e0258c664e170d3e52d20","name":"Uyen Ngo","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ngo-thieu-my-uyen-author-e1709880420317-100x100.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ngo-thieu-my-uyen-author-e1709880420317-100x100.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2024\/03\/ngo-thieu-my-uyen-author-e1709880420317-100x100.jpg","caption":"Uyen Ngo"},"url":"https:\/\/itviec.com\/blog\/author\/uyen-ngo\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/85275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/207"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=85275"}],"version-history":[{"count":2,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/85275\/revisions"}],"predecessor-version":[{"id":88211,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/85275\/revisions\/88211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/85305"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=85275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=85275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=85275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}