app.ts 1.0 KB

123456789101112131415161718192021222324252627282930
  1. function startGame() {
  2. // Starting a new game.
  3. let playerName: string = 'Audrey';
  4. logPlayer(playerName);
  5. const messagesElement: HTMLElement | null = document.getElementById('messages');
  6. // We know messagesElement cannot be null because we control the markup, so
  7. // we can use a non-null type assertion
  8. const element = messagesElement!;
  9. element.innerText = 'Welcome to MultiMath! Starting a new game';
  10. console.log('Starting new game');
  11. }
  12. function logPlayer(name: string) {
  13. console.log(`New game starting for player: ${name}.`);
  14. }
  15. function arm(doc: HTMLDocument) {
  16. // We know getElementById() cannot be null because we control the markup, so
  17. // we can use a non-null type assertion on the function call result.
  18. doc.getElementById('startGame')!.addEventListener('click', startGame);
  19. }
  20. arm(document);
  21. // Type assertions (not casting, more like in Go).
  22. let value: any = 42;
  23. let fixedString1: string = (<number>value).toFixed(2);
  24. let fixedString2: string = (value as number).toFixed(2);
  25. console.log(value, fixedString1, fixedString2);