{"id":2449,"date":"2022-06-15T17:42:33","date_gmt":"2022-06-15T10:42:33","guid":{"rendered":"https:\/\/www.marketenterprise.vn\/blog\/?p=2449"},"modified":"2022-07-04T17:49:29","modified_gmt":"2022-07-04T10:49:29","slug":"tai-sao-nen-su-dung-typescript","status":"publish","type":"post","link":"https:\/\/www.marketenterprise.vn\/blog\/tai-sao-nen-su-dung-typescript.html","title":{"rendered":"TypeScript ph\u1ea7n 1 : T\u1ea1i sao n\u00ean s\u1eed d\u1ee5ng TypeScript?"},"content":{"rendered":"<h2>Gi\u1edbi thi\u1ec7u v\u1ec1 TypeScript<\/h2>\n<p><span style=\"font-weight: 400;\">JavaScript ban \u0111\u1ea7u \u0111\u01b0\u1ee3c gi\u1edbi thi\u1ec7u nh\u01b0 l\u00e0 m\u1ed9t ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh h\u01b0\u1edbng client, sau m\u1ed9t th\u1eddi gian s\u1eed d\u1ee5ng th\u00ec c\u00e1c nh\u00e0 ph\u00e1t tri\u1ec3n nh\u1eadn ra r\u1eb1ng JavaScript c\u0169ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng h\u01b0\u1edbng server. Tuy nhi\u00ean, khi d\u1ef1 \u00e1n c\u00e0ng m\u1edf r\u1ed9ng th\u00ec m\u00e3 ngu\u1ed3n s\u1eed d\u1ee5ng JavaScript c\u00e0ng ph\u1ee9c t\u1ea1p v\u00e0 kh\u00f3 b\u1ea3o tr\u00ec v\u00ec <a href=\"https:\/\/www.javascript.com\/\" target=\"_blank\" rel=\"noopener\">JavaScript<\/a> c\u00f2n thi\u1ebfu kh\u00e1 nhi\u1ec1u t\u00ednh n\u0103ng c\u1ea7n thi\u1ebft. V\u00e0 TypeScript sinh ra \u0111\u1ec3 thu h\u1eb9p c\u00e1c thi\u1ebfu s\u00f3t n\u00e0y.<\/span><\/p>\n<h2>Nh\u1eefng t\u00ednh n\u0103ng m\u00e0 TypeScript h\u1ed7 tr\u1ee3 c\u00f2n JavaScript th\u00ec kh\u00f4ng<\/h2>\n<h3>Ki\u1ec3u c\u1ed1 \u0111\u1ecbnh cho bi\u1ebfn s\u1ed1 ho\u1eb7c k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1 t\u1eeb function (static typing)<\/h3>\n<p><span style=\"font-weight: 400;\">T\u00ednh n\u0103ng n\u00e0y gi\u00fap h\u1ea1n ch\u1ebf c\u00e1c l\u1ed7i v\u1ec1 type errors v\u00ec h\u1ea7u h\u1ebft s\u1ebd \u0111\u01b0\u1ee3c ph\u00e1t hi\u1ec7n trong qu\u00e1 tr\u00ecnh bi\u00ean d\u1ecbch. Nh\u1edd \u0111\u00f3 gi\u00fap c\u00e1c th\u00e0nh vi\u00ean trong c\u00f9ng d\u1ef1 \u00e1n hi\u1ec3u r\u00f5 h\u01a1n khi s\u1eed d\u1ee5ng m\u1ed9t bi\u1ebfn s\u1ed1 ho\u1eb7c m\u1ed9t function t\u1eeb th\u00e0nh vi\u00ean kh\u00e1c v\u00ec \u0111\u00e3 r\u00f5 ki\u1ec3u d\u1eef li\u1ec7u truy\u1ec1n v\u00e0o l\u00e0 g\u00ec, ki\u1ec3u d\u1eef li\u1ec7u tr\u1ea3 ra l\u00e0 nh\u01b0 th\u1ebf n\u00e0o.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\"> \r\nconst ColorClosing: string = \"red\"\r\nconst ColorOpening: string = \"green\"\r\n \r\nvar isClosing: boolean = true\r\n \r\nfunction btnOpen(): string {\r\n   let color: string = (isClosing) ? ColorOpening : ColorClosing\r\n   isClosing = !isClosing\r\n   return color\r\n}\r\n \r\nconsole.log(btnOpen()); \/\/ Output is green\r\nconsole.log(btnOpen()); \/\/ Output is red\r\n<\/pre>\n<h3>Interface<\/h3>\n<p><span style=\"font-weight: 400;\">R\u1ea5t h\u1eefu \u00edch \u0111\u1ec3 t\u1ea1o ra quy t\u1eafc giao ti\u1ebfp gi\u1eefa c\u00e1c th\u00e0nh ph\u1ea7n ph\u1ee9c t\u1ea1p, \u0111i\u1ec1u n\u00e0y gi\u1ea3m thi\u1ec3u r\u1ea5t nhi\u1ec1u th\u1eddi gian \u0111\u1ecdc hi\u1ec3u c\u1ee7a l\u1eadp tr\u00ecnh vi\u00ean khi s\u1eed d\u1ee5ng ch\u00fang, thay v\u00ec ph\u1ea3i \u0111\u1ecdc t\u1ea5t c\u1ea3 \u0111\u1ec3 hi\u1ec3u th\u00ec ch\u1ec9 c\u1ea7n \u0111\u1ecdc interface l\u00e0 c\u00f3 th\u1ec3 \u0111\u1ee7.\u00a0<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">interface Btn {\r\n   color: string,\r\n   click: Function\r\n}\r\n \r\nlet btn: Btn = {\r\n   color: \"red\",\r\n   click: function () {\r\n       \/\/ code here\r\n   },\r\n}\r\n \r\nconsole.log(btn) \/\/ Output is { color: 'red', click: [Function: click] }\r\n<\/pre>\n<h3>H\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (object oriented)<\/h3>\n<p><span style=\"font-weight: 400;\">Khi b\u1ea1n mu\u1ed1n mang c\u1ea3 th\u1ebf gi\u1edbi v\u00e0o c\u00e1c class th\u00ec TypeScript s\u1ebd gi\u00fap b\u1ea1n. <\/span><i><span style=\"font-weight: 400;\">V\u00e0 m\u00ecnh s\u1ebd kh\u00f4ng ph\u00e2n t\u00edch t\u00ednh \u01b0u \u0111i\u1ec3m v\u00e0 khuy\u1ebft \u0111i\u1ec3m c\u1ee7a <a href=\"https:\/\/www.marketenterprise.vn\/blog\/tinh-ke-thua-inheritance-trong-lap-trinh-huong-doi-tuong.html\">OOP<\/a> so v\u1edbi FP \u1edf \u0111\u00e2y.<\/span><\/i><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">interface Btn {\r\n   color: string,\r\n   click: Function\r\n}\r\n \r\nclass BtnCls implements Btn {\r\n   color: string = \"red\"\r\n   click(): void {\r\n       console.log(\"MEVN\")\r\n   }\r\n}\r\n \r\nlet btn: Btn = new BtnCls()\r\nconsole.log(btn) \/\/ Output is BtnCls { color: 'red' }\r\nbtn.click() \/\/ Output is MEVN<\/pre>\n<h3>Nh\u1eadn bi\u1ebft l\u1ed7i trong qu\u00e1 tr\u00ecnh bi\u00ean d\u1ecbch<\/h3>\n<p><span style=\"font-weight: 400;\">\u0110i\u1ec1u n\u00e0y s\u1ebd gi\u00fap gi\u1ea3m thi\u1ec3u l\u1ed7i trong qu\u00e1 tr\u00ecnh th\u1ef1c thi c\u1ee7a ph\u1ea7n m\u1ec1m v\u00e0 gi\u1ea3m th\u1eddi gian t\u00ecm v\u00e0 s\u1eeda l\u1ed7i.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\">let str: string\r\nstr = 1\r\n\/\/ error: Type 'number' is not assignable to type 'string'.\r\n<\/pre>\n<h2>T\u1ea1i sao n\u00ean s\u1eed d\u1ee5ng TypeScript<\/h2>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">TypeScript = JavaScript + c\u00e1c t\u00ednh n\u0103ng m\u1edf r\u1ed9ng \u2192 s\u1eed d\u1ee5ng TypeScript b\u1ea1n s\u1ebd c\u00f3 nhi\u1ec1u l\u1ef1a ch\u1ecdn h\u01a1n trong qu\u00e1 tr\u00ecnh ph\u00e1t tri\u1ec3n.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">T\u1eadn d\u1ee5ng t\u1ed1t c\u00e1c t\u00ednh n\u0103ng nh\u01b0 OOP, Interface, Static typing\u2026 s\u1ebd gi\u00fap ch\u01b0\u01a1ng tr\u00ecnh gi\u1ea3m thi\u1ec3u s\u1ef1 ph\u1ee9c t\u1ea1p.<\/span><\/li>\n<\/ul>\n<p><span>B\u1ea1n c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u r\u00f5 h\u01a1n v\u1ec1 TypeScript trong nh\u1eefng b\u00e0i vi\u1ebft d\u01b0\u1edbi \u0111\u00e2y :<\/span><\/p>\n<ul>\n<li>\n<p>TypeScript ph\u1ea7n 2 : <a href=\"https:\/\/www.marketenterprise.vn\/blog\/cai-typescript-chay-lenh-don-gian.html\">C\u00e1ch c\u00e0i \u0111\u1eb7t v\u00e0 ch\u1ea1y nh\u1eefng d\u00f2ng l\u1ec7nh \u0111\u01a1n gi\u1ea3n<\/a><\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Ch\u00e0o m\u1eebng c\u00e1c b\u1ea1n \u0111\u00e3 gh\u00e9 th\u0103m trang blog c\u1ee7a MarketEnterprise Vi\u1ec7t Nam ! H\u00f4m nay ch\u00fang ta h\u00e3y c\u00f9ng nhau t\u00ecm hi\u1ec3u v\u1ec1 ch\u1ee7 \u0111\u1ec1 \u201cT\u1ea1i sao n\u00ean s\u1eed d\u1ee5ng TypeScript?&#8221; nh\u00e9. <\/p>\n","protected":false},"author":8,"featured_media":2469,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[102],"class_list":["post-2449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-typescript"],"_links":{"self":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/2449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/comments?post=2449"}],"version-history":[{"count":0,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/posts\/2449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media\/2469"}],"wp:attachment":[{"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/media?parent=2449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/categories?post=2449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.marketenterprise.vn\/blog\/wp-json\/wp\/v2\/tags?post=2449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}