تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
بالنسبة إلى المطوّرين الذين يفضّلون كتابة الدوال في TypeScript،
تقدّم Cloud Functions نوعَين من الدعم:
إنشاء مشاريع TypeScript وضبطها لإجراء عملية التحويل التلقائي عند بدء التشغيل (firebase init functions)
تحويل رمز مصدر TypeScript الحالي إلى JavaScript في وقت النشر من خلال خطاف ما قبل النشر
باتّباع التعليمات الواردة في هذا الدليل، يمكنك نقل مشروع JavaScript حالي إلى TypeScript ومواصلة نشر الدوال باستخدام خطاف قبل النشر لتحويل الرمز المصدر.
توفّر TypeScript العديد من المزايا مقارنةً بلغة JavaScript العادية عند كتابة الدوال:
تتوافق TypeScript مع أحدث ميزات JavaScript، مثل async/await، ما يسهّل إدارة الوعود.
يُبرز مدقق Cloud Functions المشاكل الشائعة أثناء كتابة الرمز البرمجي
تساعد ميزة "أمان الأنواع" في تجنُّب أخطاء وقت التشغيل في الدوال التي تم نشرها
بدء مشروع Cloud Functions جديد باستخدام TypeScript
تشغيل firebase init functions في دليل جديد تمنحك الأداة خيارات لإنشاء المشروع باستخدام JavaScript أو TypeScript. اختَر TypeScript لإنشاء بنية المشروع التالية:
بعد اكتمال عملية الإعداد، أزِل التعليق من المثال في index.ts وشغِّل
npm run serve لرؤية دالة "Hello World" أثناء عملها.
استخدام مشروع TypeScript حالي
إذا كان لديك مشروع TypeScript حالي، يمكنك إضافة خطاف ما قبل النشر للتأكّد من ترجمة مشروعك في كل مرة تنشر فيها الرمز إلى Cloud Functions for Firebase. ستحتاج إلى ملف tsconfig.json منسَّق بشكل صحيح وإلى مشروع على Firebase، كما ستحتاج إلى إجراء التعديلات التالية على إعدادات Firebase:
عدِّل package.json لإضافة نص برمجي bash لإنشاء مشروع TypeScript. على سبيل المثال:
{"name":"functions","scripts":{"build":"npm run lint && tsc"}...
عدِّل firebase.json لإضافة خطاف ما قبل النشر لتشغيل نص الإنشاء البرمجي. على سبيل المثال:
{"functions":{"predeploy":"npm --prefix functions run build",}}
باستخدام هذا الإعداد، ينشئ الأمر firebase deploy --only functions رمز TypeScript وينشره كدوال.
نقل مشروع JavaScript حالي إلى TypeScript
إذا كان لديك مشروع Cloud Functions حالي تم إعداده وتطويره باستخدام JavaScript، يمكنك نقل البيانات إلى TypeScript. ننصحك بشدة بإنشاء نقطة حفظ في git أو نسخة احتياطية أخرى قبل البدء.
لنقل بيانات مشروع JavaScript Cloud Functions حالي، اتّبِع الخطوات التالية:
أنشئ نقطة حفظ git واحفظ نُسخًا من ملفات مصدر JavaScript الحالية.
في دليل المشروع، شغِّل firebase init functions واختَر TypeScript عندما يُطلب منك تحديد لغة لكتابة الدوال.
عندما يُطلب منك تحديد ما إذا كنت تريد استبدال ملف package.json الحالي، انقر على لا ما لم تكن متأكدًا من أنّك لا تريد الاحتفاظ بالملف الحالي.
احذف index.ts في الدليل functions/src، واستبدِله برمز المصدر الحالي.
في ملف tsconfig.json الذي تم إنشاؤه عند بدء التشغيل، اضبط خيارات المحوّل البرمجي للسماح باستخدام JavaScript: "allowJs": true.
انسخ ملف package.json المحفوظ إلى الدليل functions، وعدِّله لضبط "main" على "lib/index.js".
في package.json أيضًا، أضِف نصًا برمجيًا للإنشاء خاصًا بلغة TypeScript، مثل ما يلي:
{"name":"functions","scripts":{"build":"npm run lint && tsc"}...
أضِف "typescript" كإحدى التبعيات الخاصة بمرحلة التطوير من خلال تنفيذ npm install --save-dev typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser.
بالنسبة إلى جميع التبعيات، شغِّل npm install --save @types/<dependency>.
أعِد كتابة رمز المصدر من .js إلى .ts على النحو المطلوب.
محاكاة دوال TypeScript
لاختبار دوال TypeScript محليًا، يمكنك استخدام أدوات المحاكاة الموضّحة في تشغيل الدوال محليًا. من المهم تجميع الرمز قبل استخدام هذه الأدوات، لذا تأكَّد من تنفيذ npm run build داخل دليل الدوال قبل تنفيذ firebase emulators:start أو firebase functions:shell. بدلاً من ذلك، يمكنك تشغيل npm run serve أو npm run shell كاختصار، إذ يشغّل كلا الأمرين عملية الإنشاء ويشغّل/يبدأ واجهة الدوال.
سجلّات الدوال لمشاريع TypeScript
أثناء firebase deploy، يتم تحويل index.ts في مشروعك إلى index.js، ما يعني أنّ سجلّ Cloud Functions سيعرض أرقام الأسطر من ملف index.js وليس من الرمز الذي كتبته. لتسهيل العثور على المسارات وأرقام الأسطر المطابقة في index.ts، تنشئ firebase deployfunctions/lib/index.js.map. يمكنك استخدام خريطة المصدر هذه في بيئة التطوير المتكاملة المفضّلة لديك أو من خلال وحدة Node.
تاريخ التعديل الأخير: 2025-09-03 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-09-03 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["\u003cbr /\u003e\n\nFor developers who prefer to write functions in TypeScript,\nCloud Functions provides two types of support:\n\n- Create and configure TypeScript projects for automatic transpilation at initialization (`firebase init functions`).\n- Transpile existing TypeScript source to JavaScript at deploy time via a [predeploy hook](/docs/cli#hooks).\n\nFollowing instructions in this guide, you can migrate an existing\nJavaScript project to TypeScript and continue deploying functions using a\npredeploy hook to transpile your source code.\nTypeScript offers many benefits over vanilla JavaScript\nwhen writing functions:\n\n- TypeScript supports latest JavaScript features like async/await, simplifying promise management\n- A Cloud Functions linter highlights common problems while you're coding\n- Type safety helps you avoid runtime errors in deployed functions\n\nIf you're new to TypeScript, see [TypeScript in 5 minutes](http://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html).\n\nInitializing a new Cloud Functions project with TypeScript\n\nRun `firebase init functions` in a new directory. The tool gives you options to build\nthe project with JavaScript or TypeScript. Choose **TypeScript** to output the\nfollowing project structure: \n\n myproject\n +- functions/ # Directory containing all your functions code\n |\n +- package.json # npm package file describing your Cloud Functions code\n |\n +- tsconfig.json\n |\n +- .eslintrc.js # Optional file if you enabled ESLint\n +- tsconfig.dev.json # Optional file that references .eslintrc.js\n |\n +- src/ # Directory containing TypeScript source\n | |\n | +- index.ts # main source file for your Cloud Functions code\n |\n +- lib/\n |\n +- index.js # Built/transpiled JavaScript code\n |\n +- index.js.map # Source map for debugging\n\nOnce initialization is complete, uncomment the sample in index.ts and run\n`npm run serve` to see a \"Hello World\" function in action.\n\nUsing an existing TypeScript project\n\nIf you have an existing TypeScript project, you can add a predeploy hook to\nmake sure your project is transpiled every time you deploy your code to\nCloud Functions for Firebase. You'll need a\nproperly formed `tsconfig.json` file and a Firebase project, and you'll need\nto make the following modifications to your Firebase configuration:\n\n1. Edit `package.json` to add a bash script to build your TypeScript project. For example:\n\n {\n \"name\": \"functions\",\n \"scripts\": {\n \"build\": \"npm run lint && tsc\"\n }\n ...\n\n2. Edit `firebase.json` to add a predeploy hook to run the build script. For example:\n\n {\n \"functions\": {\n \"predeploy\": \"npm --prefix functions run build\",\n }\n }\n\nWith this configuration, a `firebase deploy --only functions` command\nbuilds your TypeScript code and deploys it as functions.\n\nMigrating an existing JavaScript project to TypeScript\n\nIf you have an existing Cloud Functions project that you initialized\nand developed in JavaScript, you can migrate it to\nTypeScript. You're strongly encouraged to create a git checkpoint or other\nbackup before starting.\n\n**To migrate an existing JavaScript Cloud Functions project:**\n\n1. Create a git checkpoint and save copies of your existing JavaScript source files.\n2. In the project directory, run `firebase init functions` and select `TypeScript` when prompted for a language for writing functions.\n3. When prompted whether to overwrite the existing `package.json` file, select **No** unless you are sure you don't want to keep the existing file.\n4. Delete `index.ts` in the directory `functions/src`, replacing it with your existing source code.\n5. In the `tsconfig.json` file created at initialization, set the compiler options to allow JavaScript: `\"allowJs\": true`.\n6. Copy your saved `package.json` file into the `functions` directory, and edit it to set `\"main\"` to `\"lib/index.js\"`.\n7. Also in `package.json`, add a build script for TypeScript like the following:\n\n {\n \"name\": \"functions\",\n \"scripts\": {\n \"build\": \"npm run lint && tsc\"\n }\n ...\n\n8. Add `\"typescript\"` as a dev dependency by running `npm install --save-dev typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser`.\n\n9. For all dependencies, run `npm install --save @types/\u003cdependency\u003e`.\n\n10. Rewrite source code from .js to .ts as desired.\n\nEmulating TypeScript functions\n\nTo test TypeScript functions locally, you can use the emulation tools described\nin [Run functions locally](/docs/functions/local-emulator). It's important to\ncompile your code before using these tools, so make sure to run `npm run build`\ninside your functions directory before running `firebase emulators:start` or\n`firebase functions:shell`. Alternatively, run `npm run serve` or\n`npm run shell` as a shortcut; these commands both run the build and\nserve/start the functions shell.\n\nFunctions logs for TypeScript projects\n\nDuring `firebase deploy`, your project's `index.ts` is transpiled to `index.js`,\nmeaning that the Cloud Functions log will output line numbers from the\n`index.js` file and not the code you wrote. To make it easier for you to find the\ncorresponding paths and line numbers in `index.ts`,\n`firebase deploy` creates `functions/lib/index.js.map`. You can use this source\nmap in your preferred IDE or via a [node module](https://github.com/evanw/node-source-map-support)."]]