Skip to content

Add TypeScript support

Michał Miłek requested to merge mmilek/destination-west-ts:main into main

This merge request lets Destination West developers write code in TypeScript instead of JavaScript if they choose to do so.

What is TypeScript?

TypeScript is a strongly typed language built on top of JavaScript. It can be described as some sort of an overlay, which enables much more advanced static analysis of your code. It extends JS with syntax for type declarations and generics and compiles to normal JS. TypeScript and JavaScript can coexist in one codebase.

Learn more here.

Benefits

  1. Bugs that would otherwise occur at runtime become apparent during compilation.
  2. Editor LSP support greatly improves. When the compiler knows types of values, it can provide correct code completion.
  3. Your code becomes more accessible to other developers as they can see the structure of it more clearly.
  4. Opportunity to learn new, widely used technologies.
  5. Expanding the codebase becomes easier with more bugs caught during refactoring/adding features.

Implications for you

Next to none! TypeScript can be introduced into the project incrementally, file by file. Most of the time, you can even change file extensions from js to ts and it will work exactly like it used to.

You won't be able to run your code using serv or some other command like this. See the "Details" section for more.

Introducing TypeScript changes nothing about existing code.

Learn TypeScript here.

Details

Since browsers cannot run TypeScript, TS code needs to be compiled first. This is done by tsc - the TypeScript compiler. Output of the compiler goes into the dist directory. Thanks to incremental building, this process is really fast when used in --watch mode. tsc looks for changes in files and recompiles your code on the fly.

I've taken steps to automate this process, so you won't experience a slowdown in your workflow. Just use a script: npm run dev and refresh the game tab when you make changes.

This merge request also modifies the GitLab Pages deploy script to accomodate for the fact that actual JS now comes from the dist directory. Docker image used has been changed to node.

Summary

You are encouraged to voice your concerns and comment about this change.

Edited by Michał Miłek

Merge request reports