We recently upgraded the version of TypeScript used for game scripting on dot big bang from TypeScript 3.7 to TypeScript 5.0. This upgrade brings a lot of new improvements and features to the script-writing experience.
These include:
- Lower CPU and memory usage when writing/compiling scripts
- Improvements to type inference
- Improvements to type narrowing
- Improvements to error and warning messaging
- New language features
Lower CPU and memory usage when writing/compiling scripts
When you write and save scripts in dot big bang, the TypeScript compiler does some work to check that your code is correct and compile it into JavaScript. While the work takes place, the CPU and RAM usage on your computer increases. If your computer has limited CPU/RAM, or you have a lot of other programs running in the background, increases to CPU/RAM usage can make programs on your computer freeze, lag, or slow down. With the new version of TypeScript, the TypeScript compiler uses much less CPU and RAM, which makes the script editing experience smoother on low-spec machines like Chromebooks.
Improvements to type inference
Although TypeScript encourages annotating your types manually by writing things like myString: string
, its designers acknowledge that there are cases where it’s convenient to omit manual type annotations, as long as the TypeScript compiler can make a good guess about what the type annotation would be, if it were present. The process where TypeScript makes a “good guess” is called “type inference”.
This upgrade brings several improvements to type inference; there are some places where TypeScript couldn’t perform inference that it now can, and there are other places where the accuracy or utility of the inferred types was improved. Smart type inference helps you feel like you aren’t fighting against TypeScript, because it can understand what you mean without you having to re-specify it as often.
Improvements to type narrowing
Type narrowing, also sometimes called type refinement, is when TypeScript leverages its knowledge of which parts of the code will run in order to automatically remove the parts of a type that are impossible to encounter in certain locations of the code. For example, consider the case where you have a variable typed as string | number
. If you use the typeof
operator in an if
statement to ensure that the variable is a string, TypeScript automatically removes the | number
part from the type of the variable within the if
statement body:
function myFunction(myInputVariable: string | number) {
// Oops, this is an error! TypeScript will put a red underline on this,
// because although strings have a `toLowerCase` method, numbers do not, and
// `myInputVariable` could be either a string or a number.
console.log(myInputVariable.toLowerCase());
if (typeof myInputVariable === "string") {
// TypeScript knows that it's impossible for code inside this block to be
// run if `myInputVariable` was `number`, so it "narrows" the type to just
// `string`. That means that using `toLowerCase` here is *NOT* an error!
console.log(myInputVariable.toLowerCase());
}
}
Upgrading TypeScript brings lots of additions to type narrowing; TypeScript is smarter about which parts of code can run when, so it can perform type narrowing in more places. This means that you will feel like you aren’t fighting against TypeScript as often, because it can understand the assertions you’ve made and use that knowledge to not error about things that won’t happen.
Improvements to error and warning messaging
When TypeScript shows error and warning messages to the user, it does its best to leverage all the information available to it in order to make the error messages as clear as possible. Additionally, TypeScript does its best to understand which code patterns are obvious mistakes, so that it can point them out to the user at authoring time instead of debugging time. This TypeScript upgrade brings several improvements to error messaging and mistake detection, which will help you understand and avoid potential bugs in your code before they happen.
New language features
This TypeScript upgrade also brings with it lots of new syntax/language features that can help you expressively and effectively author code. There are lots of new features that have been added, but here’s a hand-picked few that will be impactful to users writing code in dot big bang:
- Rest/spread in tuple types
- Getters/setters can now have different types
- Template string types
- ECMAScript 2022 private class fields/methods
static
index signatures- symbols as index types
- ECMAScript 2022 class
static
blocks - instantiation expressions
extends
constraints oninfer
type variables- variance annotations on type parameters (
in
andout
) satisfies
operatorconst
modifier on type parameters
Conclusion
This upgrade brings lots of improvements and new features to the experience of authoring game scripts on dot big bang, and also paves the way for future upgrades. We hope you enjoy using TypeScript as much as we do!
Lily Skye (Senior Software Engineer at dot big bang)