typescript-to-gdscript 0.1.2 → 0.1.3

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 (45) hide show
  1. package/dist/converter/gd-to-ts/class-scope.d.ts.map +1 -1
  2. package/dist/converter/gd-to-ts/class-scope.js +4 -4
  3. package/dist/converter/gd-to-ts/class-scope.js.map +1 -1
  4. package/dist/converter/gd-to-ts/expressions.d.ts.map +1 -1
  5. package/dist/converter/gd-to-ts/expressions.js +59 -8
  6. package/dist/converter/gd-to-ts/expressions.js.map +1 -1
  7. package/dist/converter/gd-to-ts/functions.d.ts.map +1 -1
  8. package/dist/converter/gd-to-ts/functions.js +29 -24
  9. package/dist/converter/gd-to-ts/functions.js.map +1 -1
  10. package/dist/converter/gd-to-ts/identifiers.d.ts +27 -0
  11. package/dist/converter/gd-to-ts/identifiers.d.ts.map +1 -0
  12. package/dist/converter/gd-to-ts/identifiers.js +76 -0
  13. package/dist/converter/gd-to-ts/identifiers.js.map +1 -0
  14. package/dist/converter/gd-to-ts/members.d.ts.map +1 -1
  15. package/dist/converter/gd-to-ts/members.js +15 -6
  16. package/dist/converter/gd-to-ts/members.js.map +1 -1
  17. package/dist/converter/gd-to-ts/statements.d.ts.map +1 -1
  18. package/dist/converter/gd-to-ts/statements.js +12 -1
  19. package/dist/converter/gd-to-ts/statements.js.map +1 -1
  20. package/dist/converter/gd-to-ts/type-inference.d.ts +33 -0
  21. package/dist/converter/gd-to-ts/type-inference.d.ts.map +1 -1
  22. package/dist/converter/gd-to-ts/type-inference.js +79 -0
  23. package/dist/converter/gd-to-ts/type-inference.js.map +1 -1
  24. package/dist/typings/content-generators.d.ts +2 -2
  25. package/dist/typings/content-generators.d.ts.map +1 -1
  26. package/dist/typings/content-generators.js +8 -2
  27. package/dist/typings/content-generators.js.map +1 -1
  28. package/dist/typings/godot-docs.d.ts.map +1 -1
  29. package/dist/typings/godot-docs.js +45 -16
  30. package/dist/typings/godot-docs.js.map +1 -1
  31. package/dist/typings/override-system.d.ts.map +1 -1
  32. package/dist/typings/override-system.js +8 -0
  33. package/dist/typings/override-system.js.map +1 -1
  34. package/dist/typings/scene-utils.d.ts +8 -0
  35. package/dist/typings/scene-utils.d.ts.map +1 -1
  36. package/dist/typings/scene-utils.js +49 -8
  37. package/dist/typings/scene-utils.js.map +1 -1
  38. package/dist/typings/scenes.d.ts.map +1 -1
  39. package/dist/typings/scenes.js +17 -10
  40. package/dist/typings/scenes.js.map +1 -1
  41. package/package.json +1 -1
  42. package/typings/classes/Dictionary.d.ts +73 -28
  43. package/typings/globals/globals.d.ts +20 -0
  44. package/typings-overrides/dictionary.d.ts +35 -15
  45. package/typings-overrides/object-dict.d.ts +43 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typescript-to-gdscript",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Convert TypeScript to GDScript for Godot game engine",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,7 +1,31 @@
1
1
  // AUTO-GENERATED from Godot class documentation.
2
2
  // Manual overrides applied from typings-overrides/*.d.ts
3
3
 
4
- /** A built-in data structure that holds key-value pairs. */
4
+ /**
5
+ * Override: Object (the structural base every value/dictionary inherits) —
6
+ * `this: T` generic methods so plain object literals and typed Dictionaries
7
+ * get key/value-aware typing for the Dictionary access methods.
8
+ *
9
+ * The `this: T` parameter (à la CallableFunction.apply/bind) infers the actual
10
+ * receiver type at the call site, so `keyof this` resolves to the real keys
11
+ * instead of collapsing to `keyof Object`. Each method pairs a typed overload
12
+ * (literal/known keys → precise type) with a permissive fallback (`unknown`
13
+ * key/value) so dynamic runtime keys, `{}` literals, and untyped Dictionaries
14
+ * keep compiling — matching GDScript's Variant semantics where these methods
15
+ * accept/return Variant. The typed overload comes first so it wins for known
16
+ * literal keys.
17
+ *
18
+ * Value/key TYPE ENFORCEMENT for typed dictionaries comes from the index
19
+ * signature on `Dictionary<K, V>` via the `[]` operator (`d[k] = v`), not from
20
+ * these methods — the permissive fallback intentionally allows `.set()` etc.
21
+ * with any value.
22
+ *
23
+ * const d: Dictionary<string, int> = { x: 1 };
24
+ * d.get("x"); // int (typed overload)
25
+ * d.get(someStr); // unknown (fallback)
26
+ * d.keys(); // Array<string>
27
+ * d["x"] = "no"; // ERROR (index signature enforces value type)
28
+ */
5
29
  declare interface Object {
6
30
  /**
7
31
  * Assigns elements of another `dictionary` into the dictionary. Resizes the dictionary to match `dictionary`. Performs type conversions if the dictionary is typed.
@@ -14,7 +38,7 @@ declare interface Object {
14
38
  * By default, a **shallow** copy is returned: all nested {@link Array}, {@link Dictionary}, and {@link Resource} keys and values are shared with the original dictionary. Modifying any of those in one dictionary will also affect them in the other.
15
39
  * If `deep` is `true`, a **deep** copy is returned: all nested arrays and dictionaries are also duplicated (recursively). Any {@link Resource} is still shared with the original dictionary, though.
16
40
  */
17
- duplicate(deep?: boolean): Dictionary;
41
+ duplicate<T>(this: T, deep?: boolean): T;
18
42
  /**
19
43
  * Duplicates this dictionary, deeply, like {@link duplicate} when passing `true`, with extra control over how subresources are handled.
20
44
  * `deep_subresources_mode` must be one of the values from {@link Resource.DeepDuplicateMode}. By default, only internal resources will be duplicated (recursively).
@@ -24,20 +48,24 @@ declare interface Object {
24
48
  * Removes the dictionary entry by key, if it exists. Returns `true` if the given `key` existed in the dictionary, otherwise `false`.
25
49
  * **Note:** Do not erase entries while iterating over the dictionary. You can iterate over the {@link keys} array instead.
26
50
  */
51
+ erase<T, K extends keyof T>(this: T, key: K): boolean;
27
52
  erase(key: unknown): boolean;
28
53
  /**
29
54
  * Finds and returns the first key whose associated value is equal to `value`, or `null` if it is not found.
30
55
  * **Note:** `null` is also a valid key. If inside the dictionary, {@link find_key} may give misleading results.
31
56
  */
57
+ find_key<T>(this: T, value: T[keyof T]): keyof T | null;
32
58
  find_key(value: unknown): unknown;
33
59
  /**
34
60
  * Returns the corresponding value for the given `key` in the dictionary. If the `key` does not exist, returns `default`, or `null` if the parameter is omitted.
35
61
  * **Note:** If the `default` argument is computationally expensive or has unwanted side effects, consider using the {@link has} method instead:
36
62
  */
63
+ get<T, K extends keyof T>(this: T, key: K, default_?: T[K]): T[K];
37
64
  get(key: unknown, default_?: unknown): unknown;
38
65
  /**
39
66
  * Gets a value and ensures the key is set. If the `key` exists in the dictionary, this behaves like {@link get}. Otherwise, the `default` value is inserted into the dictionary and returned.
40
67
  */
68
+ get_or_add<T, K extends keyof T>(this: T, key: K, default_?: T[K]): T[K];
41
69
  get_or_add(key: unknown, default_?: unknown): unknown;
42
70
  /**
43
71
  * Returns the built-in {@link Variant} type of the typed dictionary's keys as a {@link Variant.Type} constant. If the keys are not typed, returns {@link TYPE_NIL}. See also {@link is_typed_key}.
@@ -68,6 +96,7 @@ declare interface Object {
68
96
  * In GDScript, this is equivalent to the `in` operator:
69
97
  * **Note:** This method returns `true` as long as the `key` exists, even if its corresponding value is `null`.
70
98
  */
99
+ has<T, K extends keyof T>(this: T, key: K): boolean;
71
100
  has(key: unknown): boolean;
72
101
  /** Returns `true` if the dictionary contains all keys in the given `keys` array. */
73
102
  has_all(keys: Array<unknown>): boolean;
@@ -98,7 +127,7 @@ declare interface Object {
98
127
  /** Returns `true` if the dictionary's values are typed. */
99
128
  is_typed_value(): boolean;
100
129
  /** Returns the list of keys in the dictionary. */
101
- keys(): Array<unknown>;
130
+ keys<T>(this: T): Array<keyof T>;
102
131
  /**
103
132
  * Makes the dictionary read-only, i.e. disables modification of the dictionary's contents. Does not apply to nested content, e.g. content of nested dictionaries.
104
133
  */
@@ -112,7 +141,7 @@ declare interface Object {
112
141
  * Returns a copy of this dictionary merged with the other `dictionary`. By default, duplicate keys are not copied over, unless `overwrite` is `true`. See also {@link merge}.
113
142
  * This method is useful for quickly making dictionaries with default values:
114
143
  */
115
- merged(dictionary: Dictionary, overwrite?: boolean): Dictionary;
144
+ merged<T>(this: T, dictionary: T, overwrite?: boolean): T;
116
145
  /**
117
146
  * Returns `true` if the two dictionaries contain the same keys and values, inner {@link Dictionary} and {@link Array} keys and values are compared recursively.
118
147
  */
@@ -120,6 +149,7 @@ declare interface Object {
120
149
  /**
121
150
  * Sets the value of the element at the given `key` to the given `value`. Returns `true` if the value is set successfully. Fails and returns `false` if the dictionary is read-only, or if `key` and `value` don't match the dictionary's types. This is the same as using the `[]` operator (`dict[key] = value`).
122
151
  */
152
+ set<T, K extends keyof T>(this: T, key: K, value: T[K]): boolean;
123
153
  set(key: unknown, value: unknown): boolean;
124
154
  /**
125
155
  * Returns the number of entries in the dictionary. Empty dictionaries (`{ }`) always return `0`. See also {@link is_empty}.
@@ -131,7 +161,7 @@ declare interface Object {
131
161
  */
132
162
  sort(): void;
133
163
  /** Returns the list of values in this dictionary. */
134
- values(): Array<unknown>;
164
+ values<T>(this: T): Array<T[keyof T]>;
135
165
 
136
166
  // Operator overloads
137
167
  [__ops_ne]: { right: Dictionary; ret: boolean };
@@ -141,41 +171,56 @@ declare interface Object {
141
171
  }
142
172
 
143
173
  /**
144
- * Override: Dictionary<K, V> — typed key/value dictionary.
174
+ * Override: Dictionary typed surface.
145
175
  *
146
- * Extends the non-generic Object interface (which provides Dictionary methods
147
- * for {} literals) with generic overloads for typed dictionaries:
148
- * var scores: Dictionary<string, int> = Dictionary();
149
- * scores.set("player", 100); // OK key is string, value is int
150
- * scores.get("player"); // returns int
151
- * scores.keys(); // returns Array<string>
176
+ * `DictionaryTypedMethods<K, V>` is the single source of truth for the
177
+ * key/value-typed access methods. The generator derives `DictionaryKeyMethods`
178
+ * from it — `Omit<Object, keyof DictionaryTypedMethods> & DictionaryTypedMethods`
179
+ * so the method-name list lives only here. Because the typed methods are
180
+ * all-`unknown` when K = V = unknown, a bare `Dictionary`
181
+ * (= DictionaryKeyMethods<unknown, unknown>) still accepts `{}` literals.
152
182
  */
153
- interface Dictionary<K = unknown, V = unknown> extends Object {
154
- assign(dictionary: Dictionary<K, V>): void;
155
- duplicate(deep?: boolean): Dictionary<K, V>;
156
- duplicate_deep(deep_subresources_mode?: int): Dictionary<K, V>;
157
- erase(key: K): boolean;
158
- find_key(value: V): K | null;
183
+ interface DictionaryTypedMethods<K = unknown, V = unknown> {
159
184
  get(key: K, default_?: V): V;
160
185
  get_or_add(key: K, default_?: V): V;
186
+ set(key: K, value: V): boolean;
161
187
  has(key: K): boolean;
162
188
  has_all(keys: Array<K>): boolean;
189
+ erase(key: K): boolean;
190
+ find_key(value: V): K | null;
163
191
  keys(): Array<K>;
192
+ values(): Array<V>;
193
+ assign(dictionary: Dictionary<K, V>): void;
164
194
  merge(dictionary: Dictionary<K, V>, overwrite?: boolean): void;
165
195
  merged(dictionary: Dictionary<K, V>, overwrite?: boolean): Dictionary<K, V>;
166
- set(key: K, value: V): boolean;
167
- values(): Array<V>;
196
+ duplicate(deep?: boolean): Dictionary<K, V>;
168
197
  }
198
+ interface DictionaryKeyMethods<K = unknown, V = unknown>
199
+ extends Omit<Object, keyof DictionaryTypedMethods>,
200
+ DictionaryTypedMethods<K, V> {}
201
+ type Dictionary<K = unknown, V = unknown> =
202
+ [K] extends [string | number]
203
+ ? { [P in K & (string | number)]: V }
204
+ : DictionaryKeyMethods<K, V>;
205
+ /**
206
+ * Override: DictionaryConstructor — generic call signatures so
207
+ * `Dictionary<K, V>()` yields a typed dictionary. (Inference from a contextual
208
+ * type or a copy argument can't flow through the conditional `Dictionary` alias,
209
+ * so use explicit type arguments for a typed result.)
210
+ */
169
211
  declare interface DictionaryConstructor {
170
212
  readonly prototype: Dictionary;
171
- /** Constructs an empty {@link Dictionary}. */
172
- (): Dictionary;
173
- /**
174
- * Creates a typed dictionary from the `base` dictionary. A typed dictionary can only contain keys and values of the given types, or that inherit from the given classes, as described by this constructor's parameters.
175
- */
176
- (base: Dictionary, key_type: int, key_class_name: string, key_script: unknown, value_type: int, value_class_name: string, value_script: unknown): Dictionary;
177
- /** Returns the same dictionary as `from`. If you need a copy of the dictionary, use {@link duplicate}. */
178
- (from_: Dictionary): Dictionary;
213
+ <K = unknown, V = unknown>(): Dictionary<K, V>;
214
+ <K = unknown, V = unknown>(from_: Dictionary<K, V>): Dictionary<K, V>;
215
+ <K = unknown, V = unknown>(
216
+ base: Dictionary<K, V>,
217
+ key_type: int,
218
+ key_class_name: string,
219
+ key_script: unknown,
220
+ value_type: int,
221
+ value_class_name: string,
222
+ value_script: unknown,
223
+ ): Dictionary<K, V>;
179
224
  }
180
225
  declare const Dictionary: DictionaryConstructor;
181
226
  declare var Object: typeof GodotObject;
@@ -58,6 +58,26 @@ type ConstructorParameters<T extends abstract new (...args: any) => any> =
58
58
  T extends abstract new (...args: infer P) => any ? P : never;
59
59
  type InstanceType<T extends abstract new (...args: any) => any> =
60
60
  T extends abstract new (...args: any) => infer R ? R : any;
61
+ type ThisParameterType<T> = T extends (this: infer U, ...args: never) => any
62
+ ? U
63
+ : unknown;
64
+ type OmitThisParameter<T> = unknown extends ThisParameterType<T>
65
+ ? T
66
+ : T extends (...args: infer A) => infer R
67
+ ? (...args: A) => R
68
+ : T;
69
+ interface ThisType<T> {}
70
+ type Awaited<T> = T extends null | undefined
71
+ ? T
72
+ : T extends object & { then(onfulfilled: infer F, ...args: infer _): any }
73
+ ? F extends (value: infer V, ...args: infer _) => any
74
+ ? Awaited<V>
75
+ : never
76
+ : T;
77
+ type Uppercase<S extends string> = intrinsic;
78
+ type Lowercase<S extends string> = intrinsic;
79
+ type Capitalize<S extends string> = intrinsic;
80
+ type Uncapitalize<S extends string> = intrinsic;
61
81
  type NoInfer<T> = intrinsic;
62
82
  type IsAny<T> = boolean extends (T extends never ? true : false) ? true : false;
63
83
 
@@ -1,26 +1,46 @@
1
1
  /**
2
- * Override: Dictionary<K, V> — typed key/value dictionary.
2
+ * Override: Dictionary typed surface.
3
3
  *
4
- * Extends the non-generic Object interface (which provides Dictionary methods
5
- * for {} literals) with generic overloads for typed dictionaries:
6
- * var scores: Dictionary<string, int> = Dictionary();
7
- * scores.set("player", 100); // OK key is string, value is int
8
- * scores.get("player"); // returns int
9
- * scores.keys(); // returns Array<string>
4
+ * `DictionaryTypedMethods<K, V>` is the single source of truth for the
5
+ * key/value-typed access methods. The generator derives `DictionaryKeyMethods`
6
+ * from it — `Omit<Object, keyof DictionaryTypedMethods> & DictionaryTypedMethods`
7
+ * so the method-name list lives only here. Because the typed methods are
8
+ * all-`unknown` when K = V = unknown, a bare `Dictionary`
9
+ * (= DictionaryKeyMethods<unknown, unknown>) still accepts `{}` literals.
10
10
  */
11
- interface Dictionary<K = unknown, V = unknown> extends Object {
12
- assign(dictionary: Dictionary<K, V>): void;
13
- duplicate(deep?: boolean): Dictionary<K, V>;
14
- duplicate_deep(deep_subresources_mode?: int): Dictionary<K, V>;
15
- erase(key: K): boolean;
16
- find_key(value: V): K | null;
11
+ interface DictionaryTypedMethods<K = unknown, V = unknown> {
17
12
  get(key: K, default_?: V): V;
18
13
  get_or_add(key: K, default_?: V): V;
14
+ set(key: K, value: V): boolean;
19
15
  has(key: K): boolean;
20
16
  has_all(keys: Array<K>): boolean;
17
+ erase(key: K): boolean;
18
+ find_key(value: V): K | null;
21
19
  keys(): Array<K>;
20
+ values(): Array<V>;
21
+ assign(dictionary: Dictionary<K, V>): void;
22
22
  merge(dictionary: Dictionary<K, V>, overwrite?: boolean): void;
23
23
  merged(dictionary: Dictionary<K, V>, overwrite?: boolean): Dictionary<K, V>;
24
- set(key: K, value: V): boolean;
25
- values(): Array<V>;
24
+ duplicate(deep?: boolean): Dictionary<K, V>;
25
+ }
26
+
27
+ /**
28
+ * Override: DictionaryConstructor — generic call signatures so
29
+ * `Dictionary<K, V>()` yields a typed dictionary. (Inference from a contextual
30
+ * type or a copy argument can't flow through the conditional `Dictionary` alias,
31
+ * so use explicit type arguments for a typed result.)
32
+ */
33
+ declare interface DictionaryConstructor {
34
+ readonly prototype: Dictionary;
35
+ <K = unknown, V = unknown>(): Dictionary<K, V>;
36
+ <K = unknown, V = unknown>(from_: Dictionary<K, V>): Dictionary<K, V>;
37
+ <K = unknown, V = unknown>(
38
+ base: Dictionary<K, V>,
39
+ key_type: int,
40
+ key_class_name: string,
41
+ key_script: unknown,
42
+ value_type: int,
43
+ value_class_name: string,
44
+ value_script: unknown,
45
+ ): Dictionary<K, V>;
26
46
  }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Override: Object (the structural base every value/dictionary inherits) —
3
+ * `this: T` generic methods so plain object literals and typed Dictionaries
4
+ * get key/value-aware typing for the Dictionary access methods.
5
+ *
6
+ * The `this: T` parameter (à la CallableFunction.apply/bind) infers the actual
7
+ * receiver type at the call site, so `keyof this` resolves to the real keys
8
+ * instead of collapsing to `keyof Object`. Each method pairs a typed overload
9
+ * (literal/known keys → precise type) with a permissive fallback (`unknown`
10
+ * key/value) so dynamic runtime keys, `{}` literals, and untyped Dictionaries
11
+ * keep compiling — matching GDScript's Variant semantics where these methods
12
+ * accept/return Variant. The typed overload comes first so it wins for known
13
+ * literal keys.
14
+ *
15
+ * Value/key TYPE ENFORCEMENT for typed dictionaries comes from the index
16
+ * signature on `Dictionary<K, V>` via the `[]` operator (`d[k] = v`), not from
17
+ * these methods — the permissive fallback intentionally allows `.set()` etc.
18
+ * with any value.
19
+ *
20
+ * const d: Dictionary<string, int> = { x: 1 };
21
+ * d.get("x"); // int (typed overload)
22
+ * d.get(someStr); // unknown (fallback)
23
+ * d.keys(); // Array<string>
24
+ * d["x"] = "no"; // ERROR (index signature enforces value type)
25
+ */
26
+ interface Object {
27
+ get<T, K extends keyof T>(this: T, key: K, default_?: T[K]): T[K];
28
+ get(key: unknown, default_?: unknown): unknown;
29
+ get_or_add<T, K extends keyof T>(this: T, key: K, default_?: T[K]): T[K];
30
+ get_or_add(key: unknown, default_?: unknown): unknown;
31
+ has<T, K extends keyof T>(this: T, key: K): boolean;
32
+ has(key: unknown): boolean;
33
+ erase<T, K extends keyof T>(this: T, key: K): boolean;
34
+ erase(key: unknown): boolean;
35
+ set<T, K extends keyof T>(this: T, key: K, value: T[K]): boolean;
36
+ set(key: unknown, value: unknown): boolean;
37
+ find_key<T>(this: T, value: T[keyof T]): keyof T | null;
38
+ find_key(value: unknown): unknown;
39
+ keys<T>(this: T): Array<keyof T>;
40
+ values<T>(this: T): Array<T[keyof T]>;
41
+ duplicate<T>(this: T, deep?: boolean): T;
42
+ merged<T>(this: T, dictionary: T, overwrite?: boolean): T;
43
+ }