githubEdit

TypeScript Essentials

Learn TypeScript basics for web development with Needle Engine

TypeScript • JavaScript

Learn the fundamentals of TypeScript and JavaScript—the languages that power Needle Engine in the browser.

:::tip Coming from Unity? If you're a Unity developer familiar with C#, check out For Unity Developersarrow-up-right for a comparison-focused guide. :::

:::tip Already know TypeScript? Skip ahead to Needle Engine Scriptingarrow-up-right to start building! :::


What is TypeScript?

TypeScript is a programming language that adds type safety to JavaScript. It helps you catch errors before your code runs, making development faster and more reliable.

Key benefits:

  • 🛡️ Catch errors early - Find mistakes while writing code, not after deployment

  • 🔍 Better editor support - Autocomplete, inline documentation, refactoring tools

  • 📚 Self-documenting - Types make code easier to understand and maintain

  • 🌐 Runs everywhere - Compiles to JavaScript, works in all browsers

:::info How It Works TypeScript code is compiled to JavaScript before running in the browser. The TypeScript compiler checks your code for errors during this compilation step, then outputs plain JavaScript that browsers can execute. :::


Learning Resources

For beginners:

Official documentation:


JavaScript vs TypeScript

JavaScript

JavaScript is the language that runs in all web browsers. It's dynamically typed, meaning:

Example of JavaScript's flexibility:

This code is valid JavaScript syntax, but crashes when it runs in the browser.

Characteristics:

  • ✅ Very flexible - variables can change types

  • ❌ Errors only appear when code runs

  • ❌ No autocomplete for object properties

  • ❌ Easy to make typos that cause runtime crashes

TypeScript

TypeScript adds type checking to JavaScript. It catches errors before your code runs:

Same example in TypeScript:

TypeScript shows you the error while you type, not when users visit your site.

Characteristics:

  • ✅ Catches errors before deployment

  • ✅ Full autocomplete and inline documentation

  • ✅ Refactoring tools (rename symbols safely)

  • ✅ Self-documenting code

:::tip Why We Use TypeScript TypeScript helps you find bugs before users do. It's especially valuable for larger projects and teams, where catching errors early saves hours of debugging. :::

Understanding Types

Types describe what kind of data a variable can hold. Think of them as labels that help prevent mistakes.

Common Types

Basic types:

Type inference - TypeScript can figure out types automatically:

Why Types Matter

Types prevent common mistakes:

:::warning Don't Bypass Type Safety You might see //@ts-ignore or any types in code. These disable type checking and should be avoided—they remove the safety that TypeScript provides! :::

Type Annotations

Sometimes you need to explicitly declare a type:

Variables

Variables store data that your program can use and change. In TypeScript, you declare variables with let or const.

let - Changeable Variables

Use let when the value will change:

const - Fixed Variables

Use const when the value shouldn't change:

:::tip When to Use const Use const by default. Only use let when you know the variable needs to change. This makes your code clearer and prevents accidental changes. :::

const with Objects

const prevents reassignment, but you can still modify object properties:

:::warning Avoid var You might see var in older JavaScript code. Don't use it—always use let or const instead. var has confusing scoping rules that cause bugs. Learn why →arrow-up-right :::

Imports & Modules

Modern JavaScript/TypeScript organizes code into modules—separate files that export and import functionality.

Importing Code

To use code from another file or package, you import it:

Named imports (most common):

Default imports:

Namespace imports:

Exporting Code

To make your code available to other files, export it:

:::tip Named vs Namespace Imports Named imports are preferred—they're clearer and help build tools create smaller bundles. Use namespace imports only when you need many items from one package. :::

How Objects Work (References)

In JavaScript/TypeScript, when you assign an object to a variable, you're creating a reference (like a pointer or shortcut) to that object, not a copy.

The Problem

Both position and otherPosition refer to the same object in memory.

The Solution: Clone Objects

To create a true copy, use .clone():

When to Clone

Clone when you need an independent copy:

:::tip Remember

  • Simple types (number, string, boolean) are always copied

  • Objects (including Vector3, arrays, custom classes) are references

  • Use .clone() when you need an independent copy :::

Working with Vectors

Vectors represent positions, directions, and other 3D data. In Needle Engine, we use three.js vectors.

Creating Vectors

Vector Math Operations

Unlike simple numbers, you can't use +, -, * operators with vectors. Use methods instead:

Common Vector Methods

:::warning Methods Modify the Original Most vector methods change the original vector. To keep the original, clone first:

:::

Comparing Values

JavaScript/TypeScript has two ways to check if values are equal: === and ==.

Strict Equality (===) - Use This!

Strict equality checks if values are exactly the same:

Loose Equality (==) - Rarely Needed

Loose equality performs type conversion before comparing:

Comparison Table

Expression

=== (Strict)

== (Loose)

100 === 100

✅ true

✅ true

100 === "100"

❌ false

✅ true

0 === false

❌ false

✅ true

null === undefined

❌ false

✅ true

:::tip Always Use === Use strict equality (===) by default. It's more predictable and avoids surprising type conversions. Only use == when you specifically need to check for both null and undefined together. :::

Functions and this

Functions are blocks of reusable code. In TypeScript, there are two ways to write them, and they handle this differently.

Regular Functions

Traditional function syntax:

Shorter syntax with automatic this binding:

The this Problem

In JavaScript, this can change depending on how a function is called. This causes problems with event handlers:

Solution: Arrow Functions

Arrow functions automatically bind this to your class:

When to Use Each

Use arrow functions for:

  • Event handlers

  • Callbacks

  • Methods that need access to this

Use regular functions for:

  • Top-level utility functions

  • Functions that don't use this

:::tip Keep It Simple Use arrow functions for methods in your classes—they're simpler and avoid this problems. You can always use regular functions for standalone utilities. :::


Quick Reference

Here's a cheat sheet of what you've learned:


What's Next?

Now that you understand TypeScript basics, you're ready to start building with Needle Engine!

Start creating:

Explore more:

Coming from Unity?

Last updated