Ported document.ts to TypeScript fully (after having accidentally gitignored it and deleted it before..)