نحوه کارکرد let و const در تایپ اسکریپت

نحوه تعریف متغیرها در تایپ اسکریپت بر تغییر یا عدم تغییر آن‌ها تأثیر می‌گذارد. در این مقاله قصد داریم تا تاثیر استفاده از let و const، برای تعریف متغیر در تایپ اسکریپت را باهم بررسی کنیم.

تایپ اسکریپت چگونه let را استنباط می‌کند؟

هنگامی که برای تعریف یک متغیر از کلمه کلیدی let استفاده می‌کنیم، این متغیر تعریف شده قابل تغییر است و می‌توانیم آن را مجددا تخصیص دهیم.

تایپ

AlbumGenre
AlbumGenre را در نظر می‌گیریم. ترکیبی از مقادیر literal است که نشان دهنده ژانرهای ممکن برای یک آلبوم می‌باشد:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
type AlbumGenre = "rock" | "country" | "electronic";
type AlbumGenre = "rock" | "country" | "electronic";
type AlbumGenre = "rock" | "country" | "electronic";

با استفاده از let، می‌توانیم یک متغیر

albumGenre
albumGenre تعریف کنیم و مقدار
rock
rock را به آن اختصاص دهیم. سپس می‌توانیم
albumGenre
albumGenre را به تابعی که انتظار یک
AlbumGenre
AlbumGenre را دارد، ارسال کنیم.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let albumGenre = "rock";
const handleGenre = (genre: AlbumGenre) => {
// ...
};
handleGenre(albumGenre);
Argument of type 'string' is not assignable to parameter of type 'AlbumGenre'.
let albumGenre = "rock"; const handleGenre = (genre: AlbumGenre) => { // ... }; handleGenre(albumGenre); Argument of type 'string' is not assignable to parameter of type 'AlbumGenre'.
let albumGenre = "rock";
 
const handleGenre = (genre: AlbumGenre) => {
  // ...
};
 
handleGenre(albumGenre);
Argument of type 'string' is not assignable to parameter of type 'AlbumGenre'.

از آنجایی که let هنگام تعریف متغیر استفاده می‌شود، تایپ اسکریپت می‌داند که مقدار آن را می‌تواند بعداً تغییر دهد. در این مثال،

albumGenre
albumGenre را به عنوان یک
string
string به جای تایپ literal خاص
rock
rock استنباط می‌کند. ما می‌توانیم این کار را در کد خود انجام دهیم:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
albumGenre = "country";
albumGenre = "country";
albumGenre = "country";

بنابراین، یک تایپ گسترده‌تر را استنباط می‌کند تا بتواند متغیر در حال تخصیص مجدد را تطبیق دهد.

می‌توانیم با اختصاص یک تایپ خاص به let خطای بالا را برطرف کنیم:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let albumGenre: AlbumGenre = "rock";
const handleGenre = (genre: AlbumGenre) => {
// ...
};
handleGenre(albumGenre); // no more error
let albumGenre: AlbumGenre = "rock"; const handleGenre = (genre: AlbumGenre) => { // ... }; handleGenre(albumGenre); // no more error
let albumGenre: AlbumGenre = "rock";
 
const handleGenre = (genre: AlbumGenre) => {
  // ...
};
 
handleGenre(albumGenre); // no more error

اکنون، می‌توانیم

albumGenre
albumGenre را دوباره اختصاص دهیم، اما فقط به مقداری که عضو union
AlbumGenre
AlbumGenre باشد. بنابراین، پس از ارسال به
handleGenre
handleGenre، دیگر خطایی نشان نخواهد داد.

اما برای حل این مشکل، راه حل جالب دیگری هم وجود دارد.

تایپ اسکریپت چگونه const را استنباط می‌کند؟

هنگامی که از const برای تعریف متغیر استفاده می‌کنیم، آن متغیر تغییرناپذیر می‌شود و به این ترتیب، نمی‌توانیم آن را مجددا تخصیص دهیم. وقتی که تعریف متغیر را به استفاده از const تغییر می‌دهیم، تایپ اسکریپت تایپ آن را با دقت بیشتری استنتاج می‌کند:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const albumGenre = "rock";
const handleGenre = (genre: AlbumGenre) => {
// ...
};
handleGenre(albumGenre); // No error
const albumGenre = "rock"; const handleGenre = (genre: AlbumGenre) => { // ... }; handleGenre(albumGenre); // No error
const albumGenre = "rock";
 
const handleGenre = (genre: AlbumGenre) => {
  // ...
};
 
handleGenre(albumGenre); // No error

در این مثال دیگر خطایی در انتساب وجود ندارد، و تایپ اسکریپت

albumGenre
albumGenre را به عنوان تایپ literal
rock
rock استنباط می‌کند.

اگر بخواهیم مقدار

albumGenre
albumGenre را پس از تعریف آن به عنوان const تغییر دهیم، تایپ اسکریپت یک خطا به ما نشان می‌دهد:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
albumGenre = "country";
Cannot assign to 'albumGenre' because it is a constant.
albumGenre = "country"; Cannot assign to 'albumGenre' because it is a constant.
albumGenre = "country";
Cannot assign to 'albumGenre' because it is a constant.

تایپ اسکریپت به منظور جلوگیری از خطاهای احتمالی زمان اجرا، رفتار جاوااسکریپت با const را منعکس می‌کند. وقتی متغیری را با const تعریف می‌کنیم، تایپ اسکریپت آن را به عنوان تایپ literalای که مشخص کرده‌ایم استنتاج می‌کند.

بنابراین، تایپ اسکریپت از نحوه عملکرد جاوااسکریپت به نفع خود استفاده می‌کند. این موضوع اغلب ما را تشویق می‌کند که هنگام تعریف متغیرها در تایپ اسکریپت به جای let از const استفاده کنیم، زیرا کمی سخت‌گیرانه‌تر می‌باشد.

دیدگاه‌ها:

افزودن دیدگاه جدید