چگونه برنامه های تایپ اسکریپت را در لینوکس اجرا کنیم

انتشار در ۲ خرداد ۱۴۰۵
برنامه نویسی

بیایید تمام مراحل را فقط با ترمینال لینوکس انجام دهیم. من همه دستورات را قدم به قدم می‌نویسم.

🚀 شروع از صفر در ترمینال

مرحله 1: ساخت فولدر و وارد شدن به آن

mkdir ~/typescript-practice
cd ~/typescript-practice

مرحله 2: راه‌اندازی npm و نصب TypeScript

npm init -y
npm install typescript ts-node @types/node --save-dev

مرحله 3: ایجاد فایل tsconfig.json

npx tsc --init

مرحله 4: ساخت فولدر src

mkdir src

مرحله 5: ایجاد فایل TypeScript

cat > src/index.ts << 'EOF'
type Circle = { 
  kind: 'circle'; 
  radius: number; 
};

type Rectangle = { 
  kind: 'rectangle'; 
  width: number; 
  height: number; 
};

type Shape = Circle | Rectangle;

function getArea(shape: Shape): number {
  if (shape.kind === 'circle') {
    return Math.round(Math.PI * shape.radius * shape.radius);
  } else {
    return shape.width * shape.height;
  }
}

// تست کردن
const myCircle: Circle = { kind: 'circle', radius: 5 };
const myRect: Rectangle = { kind: 'rectangle', width: 4, height: 6 };

console.log('مساحت دایره:', getArea(myCircle));
console.log('مساحت مستطیل:', getArea(myRect));
console.log('دایره با شعاع 3:', getArea({ kind: 'circle', radius: 3 }));
EOF

مرحله 6: اجرای کد

npx ts-node src/index.ts

✅ خروجی مورد انتظار:

مساحت دایره: 79
مساحت مستطیل: 24
دایره با شعاع 3: 28

📝 دستورات سریع برای کارهای روزمره:

# اجرای فایل اصلی
npx ts-node src/index.ts

# ساخت فایل جدید برای تمرین
touch src/exercise1.ts
# سپس با یک ویرایشگر مثل nano یا vim باز کنید
nano src/exercise1.ts

bash

# ساخت فایل دوم
cat > src/shapes.ts << 'EOF'
export type Circle = { kind: 'circle'; radius: number };
export type Rectangle = { kind: 'rectangle'; width: number; height: number };
export type Shape = Circle | Rectangle;

export function getArea(shape: Shape): number {
  if (shape.kind === 'circle') {
    return Math.round(Math.PI * shape.radius * shape.radius);
  }
  return shape.width * shape.height;
}
EOF

🎯 کارهایی که می‌توانید بعد از این انجام دهید:

# ایجاد فایل برای تمرین مساله مربع و مثلث
cat > src/practice.ts << 'EOF'
// تمرین: اضافه کردن Square و Triangle
type Square = { kind: 'square'; side: number };
type Triangle = { kind: 'triangle'; base: number; height: number };
type NewShape = Square | Triangle;

function getArea2(shape: NewShape): number {
  if (shape.kind === 'square') {
    return shape.side * shape.side;
  } else {
    return (shape.base * shape.height) / 2;
  }
}

console.log('مربع با ضلع 4:', getArea2({ kind: 'square', side: 4 }));
console.log('مثلث با قاعده 5 و ارتفاع 3:', getArea2({ kind: 'triangle', base: 5, height: 3 }));
EOF

# اجرای فایل تمرین
npx ts-node src/practice.ts

💡 نکات مفید ترمینال:

# دیدن لیست فایل‌ها
ls -la

# دیدن محتوای فایل
cat src/index.ts

# ویرایش سریع با nano
nano src/index.ts
# (Ctrl+O برای ذخیره، Ctrl+X برای خروج)

# پاک کردن صفحه ترمینال
clear

# اجرای مجدد آخرین دستور (برای تست مکرر)
npx ts-node src/index.ts
# بعد از تغییر کد، از کلید ↑ (بالا) استفاده کنید و Enter بزنید

🎉 جمع‌بندی:

تنها با این چند دستور، محیط کامل TypeScript را در ترمینال لینوکس دارید:

# یک بار انجام دهید:
mkdir -p ~/typescript-practice/src && cd ~/typescript-practice && npm init -y && npm install typescript ts-node @types/node --save-dev && npx tsc --init

# برای اجرا:
npx ts-node src/index.ts

همین! حالا می‌توانید هر کد TypeScript که دارید در فولدر src بگذارید و اجرا کنید. 🚀