It seems that what I am trying to do is not possible, but I really hope it is.
Essentially, I have two interfaces, and I want to annotate a single function parameter as the combination of both of them.
interface ClientRequest {
userId: number
sessionKey: string
}
interface Coords {
lat: number
long: number
}
And then, in the function, I want to do something like this:
function(data: ClientRequest&Coords) { ... }
So that my 'data' object could contain all of the members from both types.
I saw something referenced in a spec preview, under "Combining Types' Members", but it seems like this hasn't made it in yet.
If it isn't possible, my solution might look like this:
interface ClientRequest<T> {
userId: number
sessionKey: string
data?: T
}
function(data: ClientRequest<Coords>) { ... }
Which would work in this case, although it's not as dynamic as I would like. I would really like to be able to combine multiple (2+) types in the annotation itself:
function(data: TypeA&TypeB&TypeC) { ... }
I would guess that the conventional solution is to define a type that extends those types, although that seems less flexible. If I want to add a type, I would have to either
- (a) go back to the declaration and rewrite it, or
- (b) create an entirely new interface. Not sure I agree with the extra overhead.
Any TypeScript experts care to point me in the right direction?