Well looking so your concept is correct, you should extend the "Date class" (since JS does not actually use inheritance classes) and add your method in the "format" case, but I believe the error is being in typing, because Javascript is weakly typed, so you do not need to define (alias neither accepts type manipulation only conversions) and the guys he kind of understands alone.
So rewriting his code it looks something like this:
<script>
// Adiciona um método a classe "Date" e cria uma função anonima
Date.prototype.format = function(format) {
// Dias (g = Global ~ trocar todas ocorrencias)
var newFormat = format.replace(/\[d]/g, this.getDate()); // Troca o Dia
newFormat = newFormat.replace(/\[m]/g, (this.getMonth() + 1)); // Troca o Mês
newFormat = newFormat.replace(/\[Y]/g, this.getFullYear()); // Troca o ano Completo
newFormat = newFormat.replace(/\[y]/g, this.getFullYear().toString().substr(2, 4)); // Troca o ano Simples
// Horas (g = Global ~ trocar todas ocorrencias)
newFormat = newFormat.replace(/\[H]/g, this.getHours()); // Troca a Hora formato 24h
newFormat = newFormat.replace(/\[h]/g, (this.getHours() % 12 || 12)); // Troca a Hora formato 12h
newFormat = newFormat.replace(/\[i]/g, this.getMinutes()); // Troca os Minutos
newFormat = newFormat.replace(/\[s]/g, this.getSeconds()); // Troca os Segundos
// Retorna
return newFormat;
};
// Teste
var agora = new Date();
console.log(agora.format("[d]/[m]/[y] - [h]:[i]:[s]"));
console.log(agora.format("[d]/[m]/[Y] - [H]:[i]:[s]"));
console.log(agora.format("[d]/[m]/[Y] - [H]:[i]:[s] && [d]/[m]/[Y] - [H]:[i]:[s]"));
</script>
Testing on IE 11, Chorme 48+ and Firefox 43+.
Do you really want to change the prototype of Date? I believe the most "Typescript" way to do this would be to extend the Date class.
– Pablo Almeida