Learn TypeScript in Construct, part 2: language basics

UpvoteUpvote 5 DownvoteDownvote

Index

Features on these Courses

Stats

325 visits, 406 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY-NC 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 20 Jun, 2025.

Variables

A variable is a name for a value stored in the computer's memory. If you've used variables in Construct's event sheet, such as global or local variables, the concept is similar. For example you could have a variable named score storing a number for the player's score. The value can be changed, or vary, hence the name variable.

Declaring and assigning variables

In TypeScript, variables are declared with the let keyword, followed by the name of the variable, a colon, and then the type of the variable. The code below creates a variable named message. The type of this variable is string, which means text - in programming string refers to a "string of characters".

let message: string;

This is the first point at which we've come across a difference between JavaScript and TypeScript. In TypeScript you must specify the kind of data a variable can hold, and TypeScript will enforce that. JavaScript does not use types, and so the variable would be declared with just let message; and you'd be able to store any kind of data in it - even things you didn't originally intend.

A value, in this case a string (text), can be assigned to this variable using the assignment operator =.

message = "Hello world!";

Now we can log this variable to the console. Here's the full code snippet.

let message: string;
message = "Hello world!";
console.log(message);

Enforcing types

As mentioned, a key feature of TypeScript is that it enforces the type of variables. This helps ensure your code is structured correctly and prevent mistakes. For example try adding a line to assign a number to message like so:

let message: string;
message = 123;

The second line will be marked as an error. This is because you said message will store a string, but then you tried to store a number in it. TypeScript therefore reports an error type 'number' is not assignable to type 'string'. Construct also won't let you preview the project while there are outstanding TypeScript errors.

Type inference

It's very common to declare a variable and immediately assign it a value, so both can be combined in to one statement like so:

let message: string = "Hello world!";

In TypeScript, when you do this you can omit the type of the variable, and just write this:

let message = "Hello world!";

This does not mean the variable has no type. In fact it's identical to the previous declaration specifying the type string. This is because if you don't specify a type but immediately assign a value, TypeScript automatically uses the type of the value for the type of the variable. With the code editor open if you hover the mouse over message you'll see it show let message: string in the tooltip, showing how the variable still has a type of string. This doesn't work if you don't immediately assign a value, as that means there is no value for TypeScript to take the type from, and so you must explicitly specify the type.

More about variable declarations

Multiple variables can also be declared using commas, with or without an assignment. For example:

let firstName: string, lastName: string, food = "pizza";

Is equivalent to:

let firstName: string;
let lastName: string;
let food = "pizza";  // remember this infers type string

In general we recommend using the latter style with a let declaration for every variable as it is clearer.

You can assign a variable different values. The following code sets the message to First message! and then Second message!. The second assignment replaces the contents of the variable, so this will only log Second message!.

let message = "First message!";
message = "Second message!";
console.log(message);

Rules for using variables

You must declare a variable before using it. The following code will result in an error as it tries to use message before declaring it.

// Error: Variable 'message' is used before being assigned.
console.log(message);
let message = "Hello world!";

You cannot declare the same variable name twice.

let message = "First message!";
// Error: cannot redeclare block-scoped variable 'message'.
let message = "Second message!";

Variable names can only contain letters, digits, or the symbols $ and _. However the first character cannot be a digit. Also the variable name cannot be a reserved word, i.e. a keyword used by the TypeScript language, such as let.

// Valid variable names:
let userName: string;
let user_name: string;
let testing123: string;

// Invalid variable names:
let user-name: string;	// dash '-' not allowed
let 9slice: string;	// starting with digit not allowed
let let: string;	// keyword not allowed

Note also that TypeScript is case-sensitive, i.e. message is regarded as a different variable name to Message or MESSAGE. This is in contrast to Construct's event sheets, which are usually case-insensitive.

It's a good idea to always use descriptive variable names to help make your code easy to understand. There are also different naming conventions, such as whether you use underscores (first_name) or "camel case" (firstName). It doesn't matter much which you pick - just be consistent, and if you work in a team, make sure everyone agrees on the same naming style.

Constants

You can also declare a constant, which is like a variable that cannot be modified. These are declared with const instead of let. Some values never need to change when your code runs, such as the mathematical constant pi, or the maximum number of login attempts allowed. Trying to change these values should be an error, and const enforces this.

// Declare a constant.
// Note this infers the type 'number'.
const PI = 3.141592653589793;

// Error: cannot assign to 'PI' because it is a constant.
PI = 4;

The naming rules for constants are identical to variables, but a common convention is to name constants with all uppercase names to help tell them apart from variables.

Next Tutorial In Course

Learn TypeScript in Construct, part 3: operators 18:29

Learn more about the various operators you can use in TypeScript, ranging from assignment to boolean logic.

  • 0 Comments

Want to leave a comment? Login or Register an account!