3 Answers
3

TypeScript does something called declaration merging, which is explained in section 10.5 of the spec.

The gist of it is you can put members in module foo, and then later on put more members in module foo. As of 0.9 this extends to putting members in the namespace of a class as well, as long as the class is declared first. That's a new feature and I've discovered bugs around it, but it's supposed to work.

Apparently my answer is not complete because String is declared as a var and not a module. Declaration merging doesn't carry over to vars (as of 0.9) which is annoying. There is still a way around this though it's a bit of a hack:

What's going on in filea is you're putting a function on the var String, and then declaring that mynamespace.String exists with everything String in lib.d.ts has plus what you added. Then, so long as you're working in mysnamespace, references to String will assume you're talking about mynamespace.String. That doesn't really exist so you'll get good ol' String which is what you want.

Like I said it's a bit dirty, but assuming you're following decent namespace conventions you should have a top level namespace where you only have to do this once. If you want to share the String extensions as part of a library though... well you're stuck.

It works on the playground as expected, however breaking it out into two ts files and referencing the string.d.ts one does not allow you to use it as expected
– maxfridbeJul 25 '13 at 14:06

Ah you're right. The issue there is in lib.d.ts String is declared as a var (which apparently doesn't qualify for merging) instead of a module. I don't think you can redeclare a var in the same namespace, but you can declare in a different namespace. I'll amend my answer to explain it.
– Jeffery GrajkowskiJul 25 '13 at 15:35