@defold-typescript/types 0.16.3 → 0.17.1

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.
@@ -1,4 +1,6 @@
1
1
  /** @noSelfInFile */
2
+ import type { Hash, Matrix4, Quaternion, Url, Vector, Vector3, Vector4 } from "../src/core-types";
3
+
2
4
  declare global {
3
5
  /**
4
6
  * Functions for checking Defold userdata types.
@@ -10,49 +12,49 @@ declare global {
10
12
  * @param var_ - Variable to check type
11
13
  * @returns True if passed type is hash
12
14
  */
13
- function is_hash(var_: unknown): boolean;
15
+ function is_hash(var_: unknown): var_ is Hash;
14
16
  /**
15
17
  * Check if passed type is matrix4.
16
18
  *
17
19
  * @param var_ - Variable to check type
18
20
  * @returns True if passed type is matrix4
19
21
  */
20
- function is_matrix4(var_: unknown): boolean;
22
+ function is_matrix4(var_: unknown): var_ is Matrix4;
21
23
  /**
22
24
  * Check if passed type is quaternion.
23
25
  *
24
26
  * @param var_ - Variable to check type
25
27
  * @returns True if passed type is quaternion
26
28
  */
27
- function is_quat(var_: unknown): boolean;
29
+ function is_quat(var_: unknown): var_ is Quaternion;
28
30
  /**
29
31
  * Check if passed type is URL.
30
32
  *
31
33
  * @param var_ - Variable to check type
32
34
  * @returns True if passed type is URL
33
35
  */
34
- function is_url(var_: unknown): boolean;
36
+ function is_url(var_: unknown): var_ is Url;
35
37
  /**
36
38
  * Check if passed type is vector.
37
39
  *
38
40
  * @param var_ - Variable to check type
39
41
  * @returns True if passed type is vector
40
42
  */
41
- function is_vector(var_: unknown): boolean;
43
+ function is_vector(var_: unknown): var_ is Vector;
42
44
  /**
43
45
  * Check if passed type is vector3.
44
46
  *
45
47
  * @param var_ - Variable to check type
46
48
  * @returns True if passed type is vector3
47
49
  */
48
- function is_vector3(var_: unknown): boolean;
50
+ function is_vector3(var_: unknown): var_ is Vector3;
49
51
  /**
50
52
  * Check if passed type is vector4.
51
53
  *
52
54
  * @param var_ - Variable to check type
53
55
  * @returns True if passed type is vector4
54
56
  */
55
- function is_vector4(var_: unknown): boolean;
57
+ function is_vector4(var_: unknown): var_ is Vector4;
56
58
  }
57
59
  }
58
60
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defold-typescript/types",
3
- "version": "0.16.3",
3
+ "version": "0.17.1",
4
4
  "description": "TypeScript types for the Defold engine's Lua APIs.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/emit-dts.ts CHANGED
@@ -191,6 +191,25 @@ export const MAPPING_TABLE_SLOTS: ReadonlyMap<string, { key: string; value: stri
191
191
  // there.
192
192
  export const RETURN_TYPE_OVERRIDES: ReadonlyMap<string, string> = new Map([["gui.get", "unknown"]]);
193
193
 
194
+ // FQN-keyed allowlist of the `types.is_*` checks that genuinely narrow their
195
+ // argument, mapped to the `DEFOLD_TYPE_MAP` token whose interface they prove.
196
+ // Emitting these as user-defined type guards (`var_ is Vector3`) is the only way
197
+ // to narrow an engine userdata value, which `typeof` cannot. An allowlist, not a
198
+ // blanket `is_*` match: `b2d.body.is_active`, `gui.is_enabled`,
199
+ // `sound.is_music_playing`, `go.exists`, `sys.exists` are runtime-state /
200
+ // existence checks on already-typed arguments and must stay `boolean`. An
201
+ // unlisted future `types.is_*` also stays a visible `boolean` until curated here,
202
+ // matching the ARBITRARY_TABLE_SLOTS deliberate-opt-in precedent.
203
+ export const TYPE_PREDICATES: ReadonlyMap<string, string> = new Map([
204
+ ["types.is_hash", "hash"],
205
+ ["types.is_matrix4", "matrix4"],
206
+ ["types.is_quat", "quaternion"],
207
+ ["types.is_url", "url"],
208
+ ["types.is_vector", "vector"],
209
+ ["types.is_vector3", "vector3"],
210
+ ["types.is_vector4", "vector4"],
211
+ ]);
212
+
194
213
  // Element names whose `table` slot is a prose-only `array/list/table of <T>` shape
195
214
  // the field-list parser cannot read, but whose element type a human curated from
196
215
  // the doc. The value is a single element token (`T[]`) or a token list when the
@@ -1153,6 +1172,16 @@ function memberSignature(
1153
1172
  .map((p, i) => emitParameter(p, i, i >= cutoff, mapType, resolver, elementName))
1154
1173
  .join(", ");
1155
1174
  const ret = emitReturn(prepared.original.returnValues, mapType, resolver, elementName);
1175
+ const predicateToken = TYPE_PREDICATES.get(elementName);
1176
+ const soleParam = original[0];
1177
+ if (
1178
+ predicateToken !== undefined &&
1179
+ soleParam !== undefined &&
1180
+ original.length === 1 &&
1181
+ ret.type === "boolean"
1182
+ ) {
1183
+ return `${name}(${params}): ${safeParamName(soleParam.name, 0)} is ${mapType(predicateToken)};`;
1184
+ }
1156
1185
  return `${name}(${params}): ${ret.type};${ret.trailing}`;
1157
1186
  }
1158
1187