Addons
Addons from the Asset Library are written in GDScript, and you can use them from TypeScript as they are. tstogd reads the scripts under addons/ and generates typings for them, so the addon’s classes get autocomplete and type checks like the engine’s own.
Using an addon
Section titled “Using an addon”Install the addon into addons/ as you normally would, and enable it under Project Settings → Plugins if it is an editor plugin. Then run tstogd convert once, or restart tstogd watch, before you use the addon in TypeScript. That generates its typings into addons/ inside your typingsDir.
Note:
convertregenerates addon typings on every run, butwatchonly does so when it starts. After you add or update an addon, runconvertor restartwatch.
An addon class with a class_name is global, as in GDScript, so you use it without an import. A script without class_name is reached with preload(), and its type comes from the addon’s typings too:
export namespace Level { export const TextUtils = preload('res://addons/dialog_box/text_utils.gd');}
export class Level extends Node2D { _ready() { let dialog = new DialogBox(); this.add_child(dialog); dialog.show_text(Level.TextUtils.wrap('Welcome!', 20)); dialog.closed.connect(() => print('closed')); }}class_name Levelextends Node2D
const TextUtils = preload("res://addons/dialog_box/text_utils.gd")
func _ready(): var dialog = DialogBox.new() self.add_child(dialog) dialog.show_text(Level.TextUtils.wrap("Welcome!", 20)) dialog.closed.connect(func(): print("closed"))Here DialogBox is the addon’s class_name, and TypeScript checks the call to show_text and the closed signal against the addon’s code.
The addon’s own .gd files are never touched. tstogd only reads them.
When an addon’s typings are wrong
Section titled “When an addon’s typings are wrong”Addon typings come from an automatic GDScript-to-TypeScript conversion, and a third-party addon can trip it up: a missing type, a wrong parameter, a class TypeScript can’t see. You can take the typings over and fix them by hand:
-
Add the addon to
excludeintstogd.json, so tstogd stops generating its typings:{"exclude": ["addons/dialog_box/**"]} -
Move the addon’s folder out of
<typingsDir>/addons/into a folder of your own, for exampletypings-fixes/dialog_box/. Keep it outsidetsDir: tstogd converts every.tsfile there into a.gd. -
Add that folder to
includeintsconfig.json:{"include": ["node_modules/typescript-to-gdscript/typings","src/**/*.ts","src/_typings/**/*.d.ts","typings-fixes/**/*.d.ts"]} -
Fix the
.tsand.gd.d.tsfiles in it by hand. They are yours now, and no run overwrites them.
When the addon gets an update, compare its changes with your fixed copy yourself, since tstogd no longer regenerates it.
Please also open an issue with the addon and what went wrong, so the conversion can improve.
Details
Section titled “Details”generate-addon-typings in the typings reference describes the generated files. exclude is listed in Configuration.
