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 toplainobjectwithout 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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -