Not going to pretend that I know what the most of the stuff mean, or if it is even safe enough, but I've followed the MDN articles and put together this TypeScript snippet [1]. Maybe somebody could comment on it?
Also sorry for the long link. Is there any accepted way to post a shorted URL?
Edit: added a corrected version [2]
[1]: https://www.typescriptlang.org/play?#code/DYUwLgBAbiDGYHsBOE...
[2]: https://www.typescriptlang.org/play?#code/FAiGGcE8DsGMAIBmBX...