javascript - Is it possible to combine members of multiple types in a TypeScript annotation? -


it seems trying not possible, hope is.

essentially, have 2 interfaces, , want annotate single function parameter combination of both of them.

interface clientrequest {     userid:     number     sessionkey: string }  interface coords {     lat:  number     long: number } 

and then, in function, want this:

function(data: clientrequest&coords) { ... } 

so 'data' object contain of members both types.

i saw referenced in (spec preview)[https://github.com/microsoft/typescript/issues/805], under "combining types' members", seems hasn't made in yet.

if isn't possible, solution might this:

interface clientrequest<t> {     userid:     number     sessionkey: string     data?:       t }  function(data: clientrequest<coords>) { ... } 

which work in case, although it's not dynamic like. able combine multiple (2+) types in annotation itself:

function(data: typea&typeb&typec) { ... } 

i guess conventional solution define type extends types, although seems less flexible. if want add type, have either (a) go declaration , rewrite it, or (b) create entirely new interface. not sure agree overhead.

any typescript experts care point me in right direction?

the specific answer question is: no, there not single inline annotation signify combined or extended types.

the best practice problem trying solve create third type extend other two.

interface iclientrequestandcoords extends iclientrequest, icoords {}   function(data: iclientrequestandcoords)  

Comments

Popular posts from this blog

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

java - Reading data from multiple zip files and combining them to one -