@defold-typescript/types 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/api-targets.json +1 -1
  2. package/generated/b2d.d.ts +3 -0
  3. package/generated/buffer.d.ts +44 -38
  4. package/generated/builtin-messages.d.ts +1 -1
  5. package/generated/camera.d.ts +3 -0
  6. package/generated/collectionfactory.d.ts +47 -40
  7. package/generated/collectionproxy.d.ts +23 -18
  8. package/generated/crash.d.ts +3 -0
  9. package/generated/factory.d.ts +32 -24
  10. package/generated/go.d.ts +293 -293
  11. package/generated/graphics.d.ts +3 -0
  12. package/generated/gui.d.ts +303 -283
  13. package/generated/http.d.ts +26 -16
  14. package/generated/iac.d.ts +3 -0
  15. package/generated/iap.d.ts +6 -3
  16. package/generated/image.d.ts +30 -26
  17. package/generated/json.d.ts +36 -32
  18. package/generated/kinds/gui-script.d.ts +7 -5
  19. package/generated/kinds/render-script.d.ts +7 -5
  20. package/generated/kinds/script.d.ts +7 -5
  21. package/generated/label.d.ts +16 -9
  22. package/generated/liveupdate.d.ts +29 -26
  23. package/generated/model.d.ts +57 -45
  24. package/generated/msg.d.ts +12 -9
  25. package/generated/particlefx.d.ts +50 -34
  26. package/generated/physics.d.ts +153 -133
  27. package/generated/profiler.d.ts +45 -41
  28. package/generated/push.d.ts +5 -2
  29. package/generated/render.d.ts +410 -349
  30. package/generated/resource.d.ts +619 -572
  31. package/generated/socket.d.ts +49 -33
  32. package/generated/sound.d.ts +83 -72
  33. package/generated/sprite.d.ts +36 -32
  34. package/generated/sys.d.ts +198 -189
  35. package/generated/tilemap.d.ts +43 -39
  36. package/generated/timer.d.ts +42 -36
  37. package/generated/vmath.d.ts +254 -229
  38. package/generated/webview.d.ts +3 -0
  39. package/generated/window.d.ts +23 -17
  40. package/generated/zlib.d.ts +15 -12
  41. package/index.d.ts +3 -1
  42. package/package.json +6 -2
  43. package/scripts/example-store-io.ts +18 -0
  44. package/scripts/fidelity-audit.ts +61 -1
  45. package/scripts/fidelity-baseline.json +10 -10
  46. package/scripts/ref-doc-delta.ts +143 -0
  47. package/scripts/regen.ts +23 -10
  48. package/src/core-types.ts +14 -0
  49. package/src/doc-comment.ts +2 -1
  50. package/src/emit-dts.ts +238 -18
  51. package/src/engine-globals.d.ts +2 -0
  52. package/src/example-store.ts +44 -0
  53. package/src/go-overloads.d.ts +73 -0
  54. package/src/index.ts +5 -0
  55. package/src/lifecycle.ts +157 -16
  56. package/src/message-dispatch.d.ts +21 -0
  57. package/src/message-guard.d.ts +19 -0
  58. package/src/msg-overloads.d.ts +20 -0
  59. package/src/publish-dts.ts +1 -1
package/src/lifecycle.ts CHANGED
@@ -39,12 +39,32 @@ export interface InputAction {
39
39
  marked_text?: string;
40
40
  }
41
41
 
42
- export interface ScriptHooks<TSelf> {
42
+ /**
43
+ * Phantom type carried by `go.property()` descriptors.
44
+ *
45
+ * @deprecated Declare properties with the value-keyed `properties` field inside
46
+ * `defineScript({ properties })` — that form types them onto `self` directly and
47
+ * needs no descriptor. The `go.property` escape hatch still returns this for
48
+ * backward compatibility.
49
+ */
50
+ export interface ScriptProperty<TValue> {
51
+ readonly __defoldScriptProperty: TValue;
52
+ }
53
+
54
+ /**
55
+ * @deprecated Use the value-keyed `properties` field of `defineScript`; the
56
+ * descriptor-plus-`ScriptProperties` extraction is no longer needed.
57
+ */
58
+ export type ScriptProperties<T extends Record<string, ScriptProperty<unknown>>> = {
59
+ [K in keyof T]: T[K] extends ScriptProperty<infer TValue> ? TValue : never;
60
+ };
61
+
62
+ export interface ScriptHooks<TSelf, TInitState = TSelf> {
43
63
  // `init` returns the script's initial state; it is the sole site TypeScript
44
- // solves `TSelf` from. The engine owns `self` (a userdata-backed table), so
45
- // every other hook wraps it in `NoInfer<TSelf>` — otherwise their `self`
64
+ // solves `TInitState` from. The engine owns `self` (a userdata-backed table),
65
+ // so every other hook wraps it in `NoInfer<TSelf>` — otherwise their `self`
46
66
  // competes as a second inference site and `TSelf` collapses to `{}`.
47
- init?(): TSelf;
67
+ init?(): TInitState;
48
68
  update?(self: NoInfer<TSelf>, dt: number): void;
49
69
  fixed_update?(self: NoInfer<TSelf>, dt: number): void;
50
70
  late_update?(self: NoInfer<TSelf>, dt: number): void;
@@ -84,27 +104,148 @@ type Equal<A, B> =
84
104
  (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
85
105
 
86
106
  // drift-pin: SCRIPT_HOOK_NAMES must list exactly the ScriptHooks members
87
- const _hookNamesPinnedToInterface: Equal<ScriptHookName, keyof ScriptHooks<unknown>> = true;
107
+ const _hookNamesPinnedToInterface: Equal<ScriptHookName, keyof ScriptHooks<unknown, unknown>> =
108
+ true;
88
109
  void _hookNamesPinnedToInterface;
89
110
 
90
- export type GuiScriptHooks<TSelf> = ScriptHooks<TSelf>;
111
+ export type GuiScriptHooks<TSelf, TInitState = TSelf> = ScriptHooks<TSelf, TInitState>;
112
+
113
+ export type RenderScriptHooks<TSelf, TInitState = TSelf> = Omit<
114
+ ScriptHooks<TSelf, TInitState>,
115
+ "on_input"
116
+ >;
117
+
118
+ // The factory hook table plus the value-keyed `properties` field. `TProps` is
119
+ // the property channel (raw default values), `TSelf` the merged `self` the
120
+ // callbacks see (`NoInfer`-wrapped inside the hook set), and `TInitState` what
121
+ // `init` returns. `ScriptHooks` itself stays callback-only so the
122
+ // `SCRIPT_HOOK_NAMES` drift pin remains valid.
123
+ export type ScriptHooksWithProperties<TProps, TSelf, TInitState> = ScriptHooks<
124
+ TSelf,
125
+ TInitState
126
+ > & {
127
+ properties?: TProps;
128
+ };
129
+
130
+ export type GuiScriptHooksWithProperties<TProps, TSelf, TInitState> = GuiScriptHooks<
131
+ TSelf,
132
+ TInitState
133
+ > & {
134
+ properties?: TProps;
135
+ };
91
136
 
92
- export type RenderScriptHooks<TSelf> = Omit<ScriptHooks<TSelf>, "on_input">;
137
+ export type RenderScriptHooksWithProperties<TProps, TSelf, TInitState> = RenderScriptHooks<
138
+ TSelf,
139
+ TInitState
140
+ > & {
141
+ properties?: TProps;
142
+ };
93
143
 
94
- export function defineScript<TSelf = Record<never, never>>(
95
- hooks: ScriptHooks<TSelf>,
96
- ): ScriptHooks<TSelf> {
144
+ /**
145
+ * Type a `.script` component's hook table. At runtime this is an identity
146
+ * function — it returns `hooks` unchanged; its only job is typing. It infers
147
+ * `TSelf` from `init`'s return so every other hook's `self` is typed. Declare
148
+ * editor properties with the value-keyed `properties` field — the key is the
149
+ * property name and the value its default, so the value's type threads onto
150
+ * `self` alongside `init`'s state. The transpiler's `lifecycle-erasure` pass
151
+ * rewrites the top-level call into the flat `function init(self) … end` Defold
152
+ * chunk shape and synthesizes the `go.property(...)` registrations — zero
153
+ * runtime cost, nothing the engine sees changes.
154
+ *
155
+ * Accepts the full `ScriptHooks` set, all optional: `init`, `update`,
156
+ * `fixed_update`, `late_update`, `on_message`, `on_input`, `final`,
157
+ * `on_reload`.
158
+ *
159
+ * Scaffold it with the `defold-script` / `defold-script-typed` VSCode snippets
160
+ * from `defold-typescript init`.
161
+ *
162
+ * @param hooks - the `.script` lifecycle hook table to type and return.
163
+ * @returns the same `hooks` object, now typed (identity at runtime).
164
+ * @example
165
+ * ```ts
166
+ * export default defineScript({
167
+ * init() {
168
+ * return { hits: 0 };
169
+ * },
170
+ * update(self, dt) {
171
+ * self.hits += 1;
172
+ * },
173
+ * });
174
+ * ```
175
+ */
176
+ export function defineScript<TProps extends object = Record<never, never>, TInitState = TProps>(
177
+ hooks: ScriptHooksWithProperties<TProps, TProps & TInitState, TInitState>,
178
+ ): ScriptHooksWithProperties<TProps, TProps & TInitState, TInitState> {
97
179
  return hooks;
98
180
  }
99
181
 
100
- export function defineGuiScript<TSelf = Record<never, never>>(
101
- hooks: GuiScriptHooks<TSelf>,
102
- ): GuiScriptHooks<TSelf> {
182
+ /**
183
+ * Type a `.gui_script` component's hook table. Like {@link defineScript} it is
184
+ * an identity function at runtime, infers `TSelf` from `init`'s return by
185
+ * default, accepts the same value-keyed `properties` field as
186
+ * {@link defineScript}, and is erased by the transpiler's `lifecycle-erasure`
187
+ * pass into the flat Defold chunk shape.
188
+ *
189
+ * `GuiScriptHooks` is an alias of the `.script` hook set, so it accepts the same
190
+ * full set, all optional: `init`, `update`, `fixed_update`, `late_update`,
191
+ * `on_message`, `on_input`, `final`, `on_reload`.
192
+ *
193
+ * Scaffold it with the `defold-gui` / `defold-gui-typed` VSCode snippets from
194
+ * `defold-typescript init`.
195
+ *
196
+ * @param hooks - the `.gui_script` lifecycle hook table to type and return.
197
+ * @returns the same `hooks` object, now typed (identity at runtime).
198
+ * @example
199
+ * ```ts
200
+ * export default defineGuiScript({
201
+ * init() {
202
+ * return { node: gui.get_node("score") };
203
+ * },
204
+ * on_input(self, action_id, action) {
205
+ * return false;
206
+ * },
207
+ * });
208
+ * ```
209
+ */
210
+ export function defineGuiScript<TProps extends object = Record<never, never>, TInitState = TProps>(
211
+ hooks: GuiScriptHooksWithProperties<TProps, TProps & TInitState, TInitState>,
212
+ ): GuiScriptHooksWithProperties<TProps, TProps & TInitState, TInitState> {
103
213
  return hooks;
104
214
  }
105
215
 
106
- export function defineRenderScript<TSelf = Record<never, never>>(
107
- hooks: RenderScriptHooks<TSelf>,
108
- ): RenderScriptHooks<TSelf> {
216
+ /**
217
+ * Type a `.render_script` component's hook table. Like {@link defineScript} it
218
+ * is an identity function at runtime, infers `TSelf` from `init`'s return by
219
+ * default, accepts the same value-keyed `properties` field as
220
+ * {@link defineScript}, and is erased by the transpiler's `lifecycle-erasure`
221
+ * pass into the flat Defold chunk shape.
222
+ *
223
+ * `RenderScriptHooks` is `Omit<ScriptHooks, "on_input">` — render scripts do not
224
+ * receive input. It accepts the rest of the set, all optional: `init`,
225
+ * `update`, `fixed_update`, `late_update`, `on_message`, `final`, `on_reload`.
226
+ *
227
+ * Scaffold it with the `defold-render` / `defold-render-typed` VSCode snippets
228
+ * from `defold-typescript init`.
229
+ *
230
+ * @param hooks - the `.render_script` lifecycle hook table to type and return.
231
+ * @returns the same `hooks` object, now typed (identity at runtime).
232
+ * @example
233
+ * ```ts
234
+ * export default defineRenderScript({
235
+ * init() {
236
+ * return { clear: vmath.vector4(0, 0, 0, 1) };
237
+ * },
238
+ * update(self, dt) {
239
+ * render.set_render_target(render.RENDER_TARGET_DEFAULT);
240
+ * },
241
+ * });
242
+ * ```
243
+ */
244
+ export function defineRenderScript<
245
+ TProps extends object = Record<never, never>,
246
+ TInitState = TProps,
247
+ >(
248
+ hooks: RenderScriptHooksWithProperties<TProps, TProps & TInitState, TInitState>,
249
+ ): RenderScriptHooksWithProperties<TProps, TProps & TInitState, TInitState> {
109
250
  return hooks;
110
251
  }
@@ -10,6 +10,27 @@ declare global {
10
10
  // The transpiler lowers the call to a flat `message_id == hash("...")`
11
11
  // if/elseif chain (message-dispatch-lowering.ts), keeping this package free of
12
12
  // runtime Lua.
13
+ /**
14
+ * Discriminated-union dispatcher for `on_message`: takes a record of per-message
15
+ * handlers keyed by builtin message id, each receiving its `BuiltinMessages`
16
+ * payload already narrowed, and returns the `on_message` handler that routes to
17
+ * them. Reads as `on_message: onMessage<Self>({ ... })`; `self` threads via the
18
+ * explicit type argument, mirroring `defineScript<Self>`. The transpiler lowers
19
+ * it to a flat `if/elseif message_id == hash("...")` chain.
20
+ *
21
+ * @param handlers - a partial map from builtin message id to its handler; each handler's `message` is narrowed to that id's payload.
22
+ * @returns the `on_message` lifecycle handler that dispatches to the matching entry.
23
+ * @example
24
+ * ```ts
25
+ * defineScript({
26
+ * on_message: onMessage({
27
+ * contact_point_response(self, message, sender) {
28
+ * print(message.other_group);
29
+ * },
30
+ * }),
31
+ * });
32
+ * ```
33
+ */
13
34
  function onMessage<TSelf = Record<never, never>>(
14
35
  handlers: Partial<{
15
36
  [K in BuiltinMessageId]: (self: TSelf, message: BuiltinMessages[K], sender: Url) => void;
@@ -8,6 +8,25 @@ declare global {
8
8
  // untyped `message` record narrow to its `BuiltinMessages` payload. The
9
9
  // transpiler lowers the call to `message_id == hash("...")`
10
10
  // (message-guard-lowering.ts), keeping this package free of runtime Lua.
11
+ /**
12
+ * Type guard for an `on_message` handler: narrows the untyped `message` record
13
+ * to its `BuiltinMessages` payload when `message_id` matches a known builtin
14
+ * message id. The engine delivers `message_id` as a pre-hashed `Hash`, so this
15
+ * guard re-introduces the literal a discriminated union would otherwise need.
16
+ *
17
+ * @param message_id - the hashed message id `on_message` received.
18
+ * @param message - the untyped message record `on_message` received.
19
+ * @param expected - the builtin message id to test against (e.g. `"contact_point_response"`).
20
+ * @returns `true` when `message_id` matches `expected`, narrowing `message` to that payload.
21
+ * @example
22
+ * ```ts
23
+ * function on_message(this: void, message_id: Hash, message: object, sender: Url) {
24
+ * if (isMessage(message_id, message, "contact_point_response")) {
25
+ * print(message.other_group);
26
+ * }
27
+ * }
28
+ * ```
29
+ */
11
30
  function isMessage<K extends BuiltinMessageId>(
12
31
  message_id: Hash,
13
32
  message: Record<string | number, unknown>,
@@ -7,6 +7,26 @@ type MsgPostPayload<K> = K extends BuiltinMessageId
7
7
 
8
8
  declare global {
9
9
  namespace msg {
10
+ /**
11
+ * Post a message to a receiving URL. The most common case is to send messages
12
+ * to a component. If the component part of the receiver is omitted, the message
13
+ * is broadcast to all components in the game object.
14
+ * The following receiver shorthands are available:
15
+ * - `"."` the current game object
16
+ * - `"#"` the current component
17
+ * There is a 2 kilobyte limit to the message parameter table size.
18
+ *
19
+ * @param receiver - The receiver must be a string in URL-format, a URL object or a hashed string.
20
+ * @param message_id - The id must be a string or a hashed string.
21
+ * @param message - a lua table with message parameters to send.
22
+ * @example
23
+ * ```ts
24
+ * msg.post("#collisionobject", "apply_force", {
25
+ * force: vmath.vector3(0, 1000, 0),
26
+ * position: go.get_world_position(),
27
+ * });
28
+ * ```
29
+ */
10
30
  function post<K extends string>(
11
31
  receiver: string | Url | Hash,
12
32
  message_id: K,
@@ -20,7 +20,7 @@ export function wrapAsAmbientGlobal(opts: WrapOptions): string {
20
20
  const used = collectEngineTypes(opts.emitted);
21
21
  const importLine =
22
22
  used.length === 0 ? "" : `import type { ${used.join(", ")} } from "${opts.importsFrom}";\n\n`;
23
- const inner = opts.emitted.replace(/^declare\s+namespace\s+/, "namespace ").trimEnd();
23
+ const inner = opts.emitted.replace(/(^|\n)declare\s+namespace\s+/, "$1namespace ").trimEnd();
24
24
  const indented = inner
25
25
  .split("\n")
26
26
  .map((l) => (l.length === 0 ? l : ` ${l}`))