Skip to content

typescript-to-gdscript

Write Godot 4 scripts in TypeScript, with interfaces, union types and generics checked as you type. Get plain GDScript out, with the whole Godot API typed.

TypeScript types in, readable GDScript out

Section titled “TypeScript types in, readable GDScript out”
TypeScript
export type Element = 'fire' | 'ice' | 'poison';
export interface Hit {
damage: int;
element: Element;
}
export class Health extends Node {
health_changed = gd.signal<[new_health: int, element: Element]>();
@exports weakness: Element = 'fire';
@exports health: int = 100;
@onready label: Label = this.get_node('%Label');
take_hit(hit: Hit) {
let damage = hit.damage;
// A weak spot takes double damage.
if (hit.element == this.weakness) {
damage *= 2;
}
this.health = maxi(this.health - damage, 0);
this.label.text = `HP: ${this.health}`;
this.health_changed.emit(this.health, hit.element);
}
}
GDScript
class_name Health
extends Node
signal health_changed(new_health: int, element: String)
@export
var weakness: String = "fire"
@export
var health: int = 100
@onready
var label: Label = self.get_node("%Label")
func take_hit(hit):
var damage = hit.get("damage")
# A weak spot takes double damage.
if hit.get("element") == self.weakness:
damage *= 2
self.health = maxi(self.health - damage, 0)
self.label.text = "HP: " + str(self.health)
self.health_changed.emit(self.health, hit.get("element"))

Your own types

Describe your data with interfaces, union types and generics. TypeScript checks every use, and Godot gets plain dictionaries and values.

Godot typings

Every Godot class, method and signal is typed, so your editor autocompletes the engine API.

Live diagnostics

Constructs with no clean GDScript equivalent are reported in your editor, not at runtime.

Works with GDScript addons

Addons from the Asset Library can stay in GDScript. typescript-to-gdscript generates typings for them, so you can use their classes from TypeScript.

Already have a GDScript project? You can migrate it to TypeScript with the GD-to-TS converter.