جافاسكريبت - تايب سكريبت: كيف يمكن توسيع تعريف وحدة موجودة؟
لدي ملف إعلان مكتوب لحزمة npm موجودة، لكن يبدو أن طريقة واحدة لم تُعلن، حاولت إعلانها، لكن ظهرت لي رسالة خطأ. رجاءً ساعدني.
هيكل ملف d.ts الموجود:
declare module "mongoose" {
...
class Document {}
interface Document extends MongooseDocument, NodeJS.EventEmitter, ModelProperties {
increment(): this;
remove(fn?: (err: any, product: this) => void): Promise<this>;
...
}
}
أحاول إضافة طريقة deleteOne إلى واجهة Document. ملف custom.d.ts الخاص بي:
declare module "mongoose" {
interface Document {
deleteOne(fn?: (err: any, product: this) => void): Promise<this>;
}
}
لكن ما زلت أحصل على خطأ "الخاصية 'deleteOne' غير موجودة في النوع".
إليك ملف tsconfig.json الخاص بي إذا كنت تحتاج:
{
"compilerOptions": {
"module": "commonjs",
"removeComments": true,
"esModuleInterop": true,
"moduleResolution": "node",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"pretty": true,
"resolveJsonModule": true,
"sourceMap": true,
"target": "ES2018",
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
}
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist",
"**/*.spec.ts"
]
}
ملف custom.d.ts الخاص بي موجود في مجلد 'src/'.
الإجابة رقم 1
كان عليّ استخدام إعادة التصدير في حالتي لدمج التصريحات. تايبسكريبت 4.0.5
global-axios.d.ts:
export * from 'axios';
declare module 'axios' {
export interface AxiosRequestConfig {
myConfigOption?: boolean;
}
}
الإجابة رقم 2
حسنًا! الآن أعلم أن هذا السلوك متوقع من ts-node: https://github.com/TypeStrong/ts-node#missing-types
لقد قمت بتكوين إعدادات المسارات في tsconfig.json، والآن كل شيء يعمل:
"paths": {
"mongoose": ["src/custom.d.ts"],
"*": [
"node_modules/*"
]
}
