a parameter initialization is allowed only in function or constructor implementation
15,434
Because abstract
functions cannot have function bodies
and default paramters are something that can only be implemented if you have a function body.
Allowed:
abstract class Atom {
abstract foo(x:any);
}
Error:
abstract class Atom {
abstract foo(x:any=null); // Cannot have an implementation
}
Error:
abstract class Atom {
abstract foo(x:any){ // Error cannot have implementation
}
}
Allowed :
abstract class Atom {
abstract foo(x?:any);
}
Author by
Admin
Updated on July 23, 2022Comments
-
Admin over 1 year
In the following it prompts me error due to the optional parameter ("=null" part). I hope that I could declare optional parameter in abstract class,but why typescript does not allow me to do so?
abstract class Atom extends DatalogElement { abstract toStringFormula(elem: DatalogElement.StringFormat, variableMap: Collections.Dictionary<string, Collections.Dictionary<number, VariableMap>>=null): string }
-
pushkin almost 5 yearswhy are default parameters something that can only be implemented if you have a function body? Are you just saying that that's how it is now, or could the TS team reasonably add this syntax to declarations? Might be useful to see the default type in intellisense
-
Arno Hilke over 4 years@pushkin Because TypeScript has to generate some code for the function body to make default parameters work (depending on the compilation target). See medium.com/@kgrvr/… for an example.