تايب سكريبت - يُسمح بتهيئة المعامل فقط في تنفيذ الدالة أو المنشئ


في ما يلي يطلع لي خطأ بسبب المعامل الاختياري (جزء "=null"). آمل أن أتمكن من الإعلان عن معامل اختياري في الفئة المجردة، لكن لماذا لا يسمح لي TypeScript بذلك؟

abstract class Atom extends DatalogElement {
    abstract toStringFormula(elem: DatalogElement.StringFormat, variableMap: Collections.Dictionary<string, Collections.Dictionary<number, VariableMap>>=null): string 
}

الإجابة رقم 1

لأن الدوال abstract لا يمكن أن تحتوي على bodies للدوال والمعلمات الافتراضية هي شيء يمكن تنفيذه فقط إذا كان لديك جسم دالة.

مسموح :

abstract class Atom {
    abstract foo(x:any);
}

خطأ :

abstract class Atom {
    abstract foo(x:any=null); // Cannot have an implementation
}

خطأ :

abstract class Atom {
    abstract foo(x:any){ // Error cannot have implementation
    }
}

مسموح :

abstract class Atom {
    abstract foo(x?:any);
}