Infer generic type argument from parameter type of function in TypeScript -
i want create toplainobject()
function in typescript , have come working example:
function toplainobject<s extends d, d>(source: s) { return json.parse(json.stringify(source)) d; }
now can invoke function this:
interface isample {} class sample implements isample {} let plain: isample = toplainobject<sample, isample>(new sample());
now question: there way declare toplainobject
without need of first generic type argument s extends d
using first parameter type (which s
) invoke function doing:
let plain: isample = toplainobject<isample>(new sample());
the signature function toplainobject<d>(source: s extends d) { ... }
not work , results in syntax error.
maybe misunderstand after, don't see why can't do:
interface isample {} class sample implements isample {} function toplainobject<tinterface>(source: tinterface) : tinterface { return json.parse(json.stringify(source)) tinterface; } let plain: isample = toplainobject(new sample());
also sample works me no problem (typescript 1.8.10)
interface isample {} class sample implements isample {} function toplainobject<s extends d, d>(source: s) { return json.parse(json.stringify(source)) d; } let plain: isample = toplainobject(new sample());
Comments
Post a Comment