<!DOCTYPE html>
    <html lang="vi" xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#">
    <head>
<title>Cách đơn giản hóa code TypeScript bằng những kiểu tiện ích cần thiết</title>
<meta name="description" content="Cách đơn giản hóa code TypeScript bằng những kiểu tiện ích cần thiết - Savefile - Tin Tức -...">
<meta name="author" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :.">
<meta name="copyright" content=".: Nguoicodonvn2008.info - Cõi lòng người cô đơn :. [admin@nguoicodonvn2008.info]">
<meta name="robots" content="index, archive, follow, noodp">
<meta name="googlebot" content="index,archive,follow,noodp">
<meta name="msnbot" content="all,index,follow">
<meta name="generator" content="NukeViet v4.5">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta property="og:title" content="Cách đơn giản hóa code TypeScript bằng những kiểu tiện ích cần thiết">
<meta property="og:type" content="website">
<meta property="og:description" content="Savefile - Tin Tức - https&#x3A;&#x002F;&#x002F;www.nguoicodonvn2008.info&#x002F;vi&#x002F;news&#x002F;savefile&#x002F;kien-thuc-may-tinh&#x002F;cach-don-gian-hoa-code-typescript-bang-nhung-kieu-tien-ich-can-thiet-7087.html">
<meta property="og:site_name" content=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.">
<meta property="og:url" content="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-don-gian-hoa-code-typescript-bang-nhung-kieu-tien-ich-can-thiet-7087.html">
<link rel="shortcut icon" href="https://www.nguoicodonvn2008.info/favicon.ico">
<link rel="canonical" href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-don-gian-hoa-code-typescript-bang-nhung-kieu-tien-ich-can-thiet-7087.html">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/" title="Tin Tức" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/karaoke-dual/" title="Tin Tức - Karaoke Dual" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nhac-tre/" title="Tin Tức - Nhạc trẻ" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tru-tinh/" title="Tin Tức - Trữ tình" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/nuoc-ngoai/" title="Tin Tức - Nước ngoài" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/remix/" title="Tin Tức - Remix" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tam-su-tinh-yeu/" title="Tin Tức - Tâm sự tình yêu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tho-suu-tam/" title="Tin Tức - Thơ sưu tầm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/cuoc-song/" title="Tin Tức - Cuộc sống" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/phan-mem/" title="Tin Tức - Phần mềm" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/kien-thuc-may-tinh/" title="Tin Tức - Kiến thức máy tính" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/hoc-tap/" title="Tin Tức - Học tập" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/tai-lieu/" title="Tin Tức - Tài liệu" type="application/rss+xml">
<link rel="alternate" href="https://www.nguoicodonvn2008.info/vi/news/rss/de-thi/" title="Tin Tức - Đề thi" type="application/rss+xml">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css" type="text/css">
<link rel="preload" as="style" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css" type="text/css">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/language/vi.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/global.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/assets/js/site.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/news.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/main.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/custom.js" type="text/javascript">
<link rel="preload" as="script" href="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js" type="text/javascript">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/assets/css/font-awesome.min.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/bootstrap.non-responsive.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/style.non-responsive.css">
<link rel="StyleSheet" href="https://www.nguoicodonvn2008.info/themes/default/css/news.css">
<link rel="stylesheet" href="https://www.nguoicodonvn2008.info/themes/default/css/custom.css">
<style type="text/css">
	body{background: #fff;}
</style>
    </head>
    <body>
<div id="print">
	<div id="hd_print">
		<h2 class="pull-left">.&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.</h2>
		<p class="pull-right"><a title=".&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;." href="https://www.nguoicodonvn2008.info/">https://www.nguoicodonvn2008.info</a></p>
	</div>
	<div class="clear"></div>
	<hr />
	<div id="content">
		<h1>Cách đơn giản hóa code TypeScript bằng những kiểu tiện ích cần thiết</h1>
		<ul class="list-inline">
			<li>Thứ ba - 09/05/2023 01:03</li>
			<li class="hidden-print txtrequired"><em class="fa fa-print">&nbsp;</em><a title="In ra" href="javascript:;" onclick="window.print()">In ra</a></li>
			<li class="hidden-print txtrequired"><em class="fa fa-power-off">&nbsp;</em><a title="Đóng cửa sổ này" href="javascript:;" onclick="window.close()">Đóng cửa sổ này</a></li>
		</ul>
		<div class="clear"></div>
		<div id="hometext">
		</div>
				<div class="imghome">
			<img alt="Cách đơn giản hóa code TypeScript bằng những kiểu tiện ích cần thiết" src="https://st.quantrimang.com/photos/image/2023/05/09/TypeScript-code-7.jpg" width="460" class="img-thumbnail" />
		</div>
		<div class="clear"></div>
		<div id="bodytext" class="clearfix">
			<p style="text-align: justify;">Tận dụng lợi thế của các&nbsp;<strong>kiểu tiện ích của TypeScript</strong>, bạn có thể viết code mạnh mẽ và ấn tượng hơn. Dưới đây là&nbsp;<strong>cách đơn giản hóa code TypeScript nhờ những kiểu tiện ích cơ bản</strong>.</p>

<p style="text-align: justify;"><img alt="Lập trình TypeScript" data-i="0" data-src="https://st.quantrimang.com/photos/image/2023/05/09/TypeScript-code-7.jpg" data-was-processed="true" height="340" src="https://st.quantrimang.com/photos/image/2023/05/09/TypeScript-code-7.jpg" width="650" /></p>

<p style="text-align: justify;"><strong>TypeScript&nbsp;</strong>là một superset của JavaScript, cho phép bạn thêm kiểu gõ tĩnh vào codebase. Với hệ thống gõ mạnh mẽ, TypeScript cung cấp khả năng đọc code và tính năng bắt lỗi trong thời gian biên dịch.</p>

<p style="text-align: justify;">Một trong số khía cạnh có lợi nhất của TypeScript là các kiểu tiện ích được tích hợp, nó có thể đơn giản hóa quá trình viết code của bạn, giúp bạn tiết kiệm thời gian lập trình quý giá. Giờ hãy cùng Quantrimang.com điểm danh những kiểu tiện ích TypeScript cần biết, có thể giúp bạn viết code an toàn và dễ dàng.</p>

<h2 style="text-align: justify;">Kiểu tiện ích Partial</h2>

<p style="text-align: justify;">Kiểu tiện ích Partial của TypeScript, cho phép bạn bất kỳ thuộc tính của một đối tượng tùy chọn. Nó cho phép bạn tạo đối tượng bằng những thuộc tính có thể bị thiếu hoặc chưa xác định. Ví dụ:</p>

<pre id="pre0">
<code>interface User {
  name: string;
  age: number;
  email: string;
}</code></pre>

<p style="text-align: justify;">Dùng kiểu tiện ích Partial, bạn có thể tạo một kiểu mới với tất cả thuộc tính của giao diện người dùng được đặt thành tùy chọn:</p>

<pre id="pre1">
<code>type OptionalUser = Partial&lt;User&gt;</code></pre>

<p style="text-align: justify;">Giờ bạn có thể tạo một phiên bản của kiểu OptionalUser chỉ với một số thuộc tính được xác định:</p>

<pre id="pre2">
<code>const user: OptionalUser = { name: &quot;John&quot; };</code></pre>

<p style="text-align: justify;">Bạn có thể gặp phải những tình huống cần tạo một đối tượng bằng các thuộc tính tùy chọn cụ thể. Tiện ích này hữu ích, nhất là khi bạn xây dựng các biểu mẫu có nhiều trường, vì không phải tất cả đều cần thiết và ở những tình huống như thế, tiện ích một phần đóng vai trò quan trọng.</p>

<h2 style="text-align: justify;">Kiểu tiện ích Pick</h2>

<p style="text-align: justify;">Kiểu tiện ích Pick của TypeScript có thể giúp bạn tạo một kiểu mới bằng cách chỉ chọn một subset từ thuộc tính của một kiểu hiện tại. Nó hữu ích khi bạn cần thu hẹp một đối tượng lớn hơn sang chỉ còn vài thuộc tính cần thiết hoặc nếu muốn triển khai gõ chặt chẽ trên các thuộc tính mà bạn cần.</p>

<pre id="pre3">
<code>interface User { 
  location: string;
  age: number; 
  email: string;
}

type PersonWithoutEmail = Pick&lt;User, &#039;location&#039; | &#039;age&#039;&gt;;

const person: PersonWithoutEmail = { 
  location: &#039;USA&#039;, 
  age: 30 
};</code></pre>

<p style="text-align: justify;">Ví dụ, một giao diện tên User xác định 3 thuộc tính: email, age và location.</p>

<p style="text-align: justify;">Bằng cách dùng kiểu tiện ích Pick, bạn có thể trích xuất “location” và “age” lựa chọn từ UI. Kết quả nhận được là một kiểu mới tên PersonWithoutEmail.</p>

<p style="text-align: justify;">Sau đó, bạn có thre tạo một phiên bản mới của &quot;PersonWithoutEmail&quot; và chỉ gắn giá trị cho thuộc tính location và age. Vì thuộc tính email đã bị bỏ qua trong kiểu này, bạn không thể gán nó cho đối tượng.</p>

<p style="text-align: justify;">Với kiểu tiện ích Pick, bạn có thể tạo kiểu mới, chỉ kết hợp với thuộc tính mong muốn. Điều này có thể làm code trở nên dễ đọc và dễ xử lý hơn.</p>

<h2 style="text-align: justify;">Kiểu tiện ích Readonly</h2>

<p style="text-align: justify;">Kiểu readonly cho biết thuộc tính của đối tượng là không thể thay đổi sau khi tạo. Kiểu này đảm bảo tính nhất quán của dữ liệu đối tượng trong thời gian dài, nhờ đó, việc xử lý code mượt mà và an toàn hơn.</p>

<pre id="pre4">
<code>interface User {
  readonly name: string; 
  readonly age: number; 
  readonly email: string;
}</code></pre>

<p style="text-align: justify;">Dùng kiểu tiện ích Readonly, bạn có thể tạo một kiểu mới với tất cả thuộc tính giao diện người dùng được chỉ định ở dạng readonly:</p>

<pre id="pre5">
<code>type ReadonlyUser = Readonly&lt;User&gt;</code></pre>

<p style="text-align: justify;">Bạn có thể chỉ định các giá trị thuộc tính khi tạo phiên bản ReadonlyUser:</p>

<pre id="pre6">
<code>const user: ReadonlyUser = { 
  name: &quot;John&quot;, 
  age: 30, 
  email: &quot;john@example.com&quot; 
};</code></pre>

<p style="text-align: justify;">Khi đánh dấu thuộc tính của một đối tượng là readonly, không ai có thể chỉnh sửa giá trị của những thuộc tính đó:</p>

<pre id="pre7">
<code>user.name = &quot;Jane&quot;;
// Error: Cannot assign to &#039;name&#039; because it is a read-only property.</code></pre>

<p style="text-align: justify;">Kiểu Readonly đặc biệt quan trọng trong những tình huống mà bạn muốn đảm bảo một đối tượng nào đó không thay đổi. Tuy nhiên, nó được tham chiếu. Ví dụ, nếu bạn có một đối tượng nắm giữ cài đặt cấu hình quan trọng, bạn sẽ muốn đảm bảo nó không được thay đổi.</p>

<h2 style="text-align: justify;">Kiểu tiện ích Required</h2>

<p style="text-align: justify;">Kiểu này đảm bảo tất cả thuộc tính cần thiết của Object đều sẵn có, trong khi kiểu Parital mang tới sự linh hoạt hơn bằng cách cho bạn chỉ định một tập hợp con của các thuộc tính, những kiểu khác là tùy chọn. Tiện ích này hữu ích ở những tình huống có một số thuộc tính không cần thiết hoặc có thể thay đổi.</p>

<pre id="pre8">
<code>interface User {
  name: string; 
  location: number; 
  address: string;
}</code></pre>

<p style="text-align: justify;">Bằng cách dùng kiểu tiện ích Required, nó có thể tạo một kiểu mới, kết hợp tất cả thuộc tính UI, trong khi yêu cầu từng thuộc tính trong số ví dụ trên phải xuất hiện. Việc tạo phiên bản RequiredUser chỉ khả thi khi tất cả thuộc tính của nó được đặt đúng cách.</p>

<pre id="pre9">
<code>const user: RequiredUser = { 
  name: &quot;John Doe&quot;, 
  location: &quot;USA&quot;, 
  address: &quot;Kansas 9745-0622&quot; 
};</code></pre>

<p style="text-align: justify;">Dùng kiểu tiện ích Required đảm bảo tất cả thuộc tính cần thiết đều có trong đối tượng.</p>

<h2 style="text-align: justify;">Kiểu tiện ích Omit</h2>

<p style="text-align: justify;">Kiểu tiện ích Omit cho phép bạn tạo một kiểu mới bằng cách loại trừ các thuộc tính cụ thể từ một kiểu hiện có.</p>

<pre id="pre10">
<code>interface Person {
  location: string; 
  age: number;
  email: string;
}

type PersonWithoutEmail = Omit&lt;Person, &#039;email&#039;&gt;;
const person:PersonWithoutEmail = { location: &quot;USA&quot;; age : 30 };</code></pre>

<p style="text-align: justify;">Giao diện Person có 3 thuộc tính: age, location và email. Kiểu PersonWithoutEmail dùng kiểu tiện ích Omit để bao gồm mọi thuộc tính trong đối tượng Person, ngoại trừ email. Sau đó, bạn có thể tạo một phiên bản của kiểu mới này, mà không chỉ định một giá trị cho thuộc tính email.</p>

<p style="text-align: justify;">Trên đây là một số&nbsp;<strong>kiểu tiện ích&nbsp;TypeScript</strong>&nbsp;cơ bản để đơn giản hóa quá trình viết code. Hi vọng bài viết hữu ích với các bạn.</p>
		</div>
				<div id="author">
						<p>
				<strong>Nguồn tin:</strong>
				Quantrimang.com
			</p>
		</div>
	</div>
	<div id="footer" class="clearfix">
		<div id="url">
			<strong>URL của bản tin này: </strong><a href="https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-don-gian-hoa-code-typescript-bang-nhung-kieu-tien-ich-can-thiet-7087.html" title="Cách đơn giản hóa code TypeScript bằng những kiểu tiện ích cần thiết">https://www.nguoicodonvn2008.info/vi/news/savefile/kien-thuc-may-tinh/cach-don-gian-hoa-code-typescript-bang-nhung-kieu-tien-ich-can-thiet-7087.html</a>

		</div>
		<div class="clear"></div>
		<div class="copyright">
			&copy; .&#x3A; Nguoicodonvn2008.info - Cõi lòng người cô đơn &#x3A;.
		</div>
		<div id="contact">
			<a href="mailto:admin@nguoicodonvn2008.info">admin@nguoicodonvn2008.info</a>
		</div>
	</div>
</div>
        <div id="timeoutsess" class="chromeframe">
            Bạn đã không sử dụng Site, <a onclick="timeoutsesscancel();" href="https://www.nguoicodonvn2008.info/#">Bấm vào đây để duy trì trạng thái đăng nhập</a>. Thời gian chờ: <span id="secField"> 60 </span> giây
        </div>
        <div id="openidResult" class="nv-alert" style="display:none"></div>
        <div id="openidBt" data-result="" data-redirect=""></div>
		</script>
		<div class="car-top">
  <span><img src="https://www.nguoicodonvn2008.info/themes/default/images/car.png" alt=""></span>
</div>
<div id="run_cronjobs" style="visibility:hidden;display:none;"><img alt="cron" src="/index.php?second=cronjobs&amp;p=l1uL14Jn" width="1" height="1" /></div>
<script src="https://www.nguoicodonvn2008.info/assets/js/jquery/jquery.min.js"></script>
<script>var nv_base_siteurl="/",nv_lang_data="vi",nv_lang_interface="vi",nv_name_variable="nv",nv_fc_variable="op",nv_lang_variable="language",nv_module_name="news",nv_func_name="savefile",nv_is_user=0, nv_my_ofs=-4,nv_my_abbr="EDT",nv_cookie_prefix="nv4c_e856T",nv_check_pass_mstime=1738000,nv_area_admin=0,nv_safemode=0,theme_responsive=0,nv_recaptcha_ver=2,nv_recaptcha_sitekey="",nv_recaptcha_type="image",XSSsanitize=1;</script>
<script src="https://www.nguoicodonvn2008.info/assets/js/language/vi.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/DOMPurify/purify3.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/global.js"></script>
<script src="https://www.nguoicodonvn2008.info/assets/js/site.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/news.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/main.js"></script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/custom.js"></script>
<script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "Organization",
            "url": "https://www.nguoicodonvn2008.info",
            "logo": "https://www.nguoicodonvn2008.info/uploads/angel.gif"
        }
        </script>
<script src="https://www.nguoicodonvn2008.info/themes/default/js/bootstrap.min.js"></script>
<script type="text/javascript">
var $scrolltop = $('.car-top');
$scrolltop.on('click', function () {
    $('html,body').animate({
        scrollTop: 0
    }, 800);
    $(this).addClass("car-run");
    setTimeout(function(){ $scrolltop.removeClass('car-run');}, 1000);
    return false;
});
$(window).on('scroll', function ()
{ 
    if($(window).scrollTop() >= 200)
    {
        $scrolltop.addClass("show");
        $scrolltop.addClass("car-down");
    }
    else
    {
       $scrolltop.removeClass("show");
       setTimeout(function(){ $scrolltop.removeClass('car-down');}, 300);
    }
});
</script>
</body>
</html>