fix --declaration typechecking (complex case)

This fixes generation of typescriptServices.d.ts,
although this is not a current requirement
( https://github.com/Microsoft/TypeScript/issues/465 ).

I don't have a unit test yet. [EDIT:   I added a unit test]

[EDIT] This PR fixes https://github.com/Microsoft/TypeScript/issues/466.