@tsonic/js-globals 0.3.3 → 0.3.4

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 (3) hide show
  1. package/README.md +40 -81
  2. package/index.d.ts +10 -169
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,19 +1,15 @@
1
1
  # @tsonic/js-globals
2
2
 
3
- Global type definitions for Tsonic in **JS mode** (JavaScript semantics).
3
+ JavaScript-specific global type definitions for Tsonic JS mode.
4
4
 
5
- ## Purpose
6
-
7
- This package provides JavaScript built-in types and methods for TypeScript when `noLib: true` is set, enabling JavaScript-style programming in Tsonic code while compiling to C# with `Tsonic.JSRuntime`.
8
-
9
- **Key principle:** In JS mode, JavaScript array/string methods ARE available and compile to `Tsonic.JSRuntime` extension methods.
5
+ This package extends the base types from `@tsonic/globals` with full JavaScript APIs.
10
6
 
11
7
  ## Usage
12
8
 
13
- Install this package in your Tsonic project:
9
+ Install alongside `@tsonic/globals`:
14
10
 
15
11
  ```bash
16
- npm install @tsonic/js-globals
12
+ npm install @tsonic/globals @tsonic/js-globals
17
13
  ```
18
14
 
19
15
  Add to your `tsconfig.json`:
@@ -22,102 +18,65 @@ Add to your `tsconfig.json`:
22
18
  {
23
19
  "compilerOptions": {
24
20
  "noLib": true,
25
- "types": ["@tsonic/js-globals"]
21
+ "typeRoots": [
22
+ "node_modules/@tsonic/globals",
23
+ "node_modules/@tsonic/js-globals"
24
+ ]
26
25
  }
27
26
  }
28
27
  ```
29
28
 
30
- Set your `tsonic.json` mode:
31
-
32
- ```json
33
- {
34
- "mode": "js"
35
- }
36
- ```
37
-
38
29
  ## What This Provides
39
30
 
40
- ### Full JavaScript API
41
-
42
- - `Array<T>` - **WITH** all JS methods: `.length`, `.map()`, `.filter()`, `.reduce()`, etc.
43
- - `String` - **WITH** all JS methods: `.slice()`, `.includes()`, `.split()`, etc.
44
- - `Number`, `Boolean`, `Object`, `Function` - full JavaScript definitions
31
+ ### Array with full JavaScript API
32
+ - `.length`, `.push()`, `.pop()`, `.shift()`, `.unshift()`
33
+ - `.map()`, `.filter()`, `.reduce()`, `.find()`, `.findIndex()`
34
+ - `.forEach()`, `.every()`, `.some()`, `.includes()`
35
+ - `.slice()`, `.splice()`, `.concat()`, `.join()`
36
+ - `.sort()`, `.reverse()`, `.indexOf()`, `.lastIndexOf()`
37
+ - `.at()`, `.flat()`, `.flatMap()`
38
+
39
+ ### String with full JavaScript API
40
+ - `.length`, `.charAt()`, `.charCodeAt()`
41
+ - `.indexOf()`, `.lastIndexOf()`, `.includes()`
42
+ - `.startsWith()`, `.endsWith()`
43
+ - `.slice()`, `.substring()`, `.substr()`
44
+ - `.toLowerCase()`, `.toUpperCase()`, `.trim()`
45
+ - `.padStart()`, `.padEnd()`, `.repeat()`
46
+ - `.replace()`, `.split()`, `.match()`, `.search()`
47
+
48
+ ### Other JavaScript globals
49
+ - `Number` with methods and static properties
50
+ - `Boolean`, `Object`, `Function` with full APIs
51
+ - `RegExp` with `exec()`, `test()`, etc.
45
52
  - `Math` - all math functions
46
53
  - `JSON` - `parse()` and `stringify()`
47
54
  - `console` - `log()`, `error()`, `warn()`, etc.
48
- - `Promise<T>` - for async/await support
49
- - `RegExp` - regular expressions
50
- - Iterator types - for `for-of` loops
51
-
52
- ### What's NOT Included
53
-
54
- - **No DOM types** - no `Document`, `HTMLElement`, `window`, etc.
55
- - **No Node.js types** - no `Buffer`, `process`, `require`, etc.
56
- - **No Browser APIs** - no `fetch`, `localStorage`, `XMLHttpRequest`, etc.
57
-
58
- This is intentional - Tsonic compiles to native executables, not browser/Node.js environments.
55
+ - `Error` - error types
56
+ - `Map<K,V>`, `Set<T>` - collections
57
+ - `setTimeout`, `setInterval` - timers
59
58
 
60
59
  ## Example
61
60
 
62
61
  ```typescript
63
- // Array methods work just like JavaScript
64
62
  const numbers = [1, 2, 3, 4, 5];
65
-
66
- // ✅ All JavaScript array methods available
67
- console.log(numbers.length); // 5
63
+ console.log(numbers.length); // 5
68
64
  const doubled = numbers.map(x => x * 2); // [2, 4, 6, 8, 10]
69
- const evens = numbers.filter(x => x % 2 === 0); // [2, 4]
70
- const sum = numbers.reduce((a, b) => a + b, 0); // 15
71
65
 
72
- // ✅ String methods work
73
66
  const message = "Hello, Tsonic!";
74
- console.log(message.length); // 14
75
- console.log(message.slice(0, 5)); // "Hello"
67
+ console.log(message.slice(0, 5)); // "Hello"
76
68
  console.log(message.includes("Tsonic")); // true
77
69
 
78
- // Math functions
79
- const result = Math.round(3.7); // 4
80
- const random = Math.random(); // 0.0 to 1.0
81
-
82
- // ✅ JSON operations
83
- const obj = { name: "Alice", age: 30 };
84
- const json = JSON.stringify(obj); // '{"name":"Alice","age":30}'
85
- const parsed = JSON.parse(json); // { name: "Alice", age: 30 }
70
+ const result = Math.round(3.7); // 4
71
+ const json = JSON.stringify({ x: 1 }); // '{"x":1}'
86
72
  ```
87
73
 
88
74
  ## How It Works
89
75
 
90
- When you use JS mode:
91
-
92
- 1. **TypeScript sees** JavaScript APIs (via this package)
93
- 2. **Tsonic compiler emits** calls to `Tsonic.JSRuntime` extension methods
94
- 3. **C# code uses** `Tsonic.JSRuntime` package for JS semantics
95
- 4. **Result:** JavaScript behavior in a native executable
96
-
97
- Example compilation:
98
-
99
- ```typescript
100
- // TypeScript (JS mode)
101
- const nums = [1, 2, 3];
102
- const doubled = nums.map(x => x * 2);
103
- ```
104
-
105
- ```csharp
106
- // Generated C# (uses Tsonic.JSRuntime)
107
- using Tsonic.JSRuntime;
108
-
109
- var nums = new List<int> { 1, 2, 3 };
110
- var doubled = nums.JSMap(x => x * 2);
111
- ```
112
-
113
- ## Mode Switching
114
-
115
- To switch to .NET semantics (without `.length`, `.map()`, etc.), use `@tsonic/dotnet-globals` instead:
116
-
117
- 1. Uninstall this package: `npm uninstall @tsonic/js-globals`
118
- 2. Install dotnet globals: `npm install @tsonic/dotnet-globals`
119
- 3. Update `tsconfig.json`: `"types": ["@tsonic/dotnet-globals"]`
120
- 4. Update `tsonic.json`: `"mode": "dotnet"` (or omit, dotnet is default)
76
+ 1. `@tsonic/globals` provides base types (minimal Array, String, etc.)
77
+ 2. This package extends them with JavaScript methods via interface merging
78
+ 3. Tsonic compiler emits calls to `Tsonic.JSRuntime` extension methods
79
+ 4. Result: JavaScript behavior in a native executable
121
80
 
122
81
  ## License
123
82
 
package/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * @tsonic/js-globals
3
3
  *
4
- * Global type definitions for Tsonic in JS mode.
4
+ * JavaScript-specific global type definitions for Tsonic JS mode.
5
5
  *
6
- * These provide JavaScript built-in types and methods for TypeScript
7
- * when noLib: true is set, enabling JavaScript semantics in Tsonic code.
6
+ * This package extends the base types from @tsonic/globals with full
7
+ * JavaScript APIs. Use alongside @tsonic/globals in typeRoots.
8
8
  *
9
9
  * Key principle: Array<T> HAS JS members like .length and .map
10
10
  * This enables JS-style programming while compiling to C# with Tsonic.JSRuntime
@@ -17,8 +17,8 @@ import { int } from "@tsonic/types";
17
17
 
18
18
  declare global {
19
19
  /**
20
- * Array type with full JavaScript API
21
- * In JS mode, these compile to Tsonic.JSRuntime extension methods
20
+ * Array type - extends base with full JavaScript API
21
+ * These compile to Tsonic.JSRuntime extension methods
22
22
  */
23
23
  interface Array<T> {
24
24
  /**
@@ -26,11 +26,6 @@ declare global {
26
26
  */
27
27
  length: int;
28
28
 
29
- /**
30
- * Returns the item located at the specified index.
31
- */
32
- [n: number]: T;
33
-
34
29
  /**
35
30
  * Appends new elements to the end of an array, and returns the new length.
36
31
  */
@@ -150,16 +145,10 @@ declare global {
150
145
  * Calls a defined callback function on each element of an array, and then flattens the result by one level.
151
146
  */
152
147
  flatMap<U>(callback: (value: T, index: int, array: T[]) => U | U[]): U[];
153
-
154
- /**
155
- * Returns an iterator over the array elements.
156
- */
157
- [Symbol.iterator](): IterableIterator<T>;
158
148
  }
159
149
 
160
150
  interface ReadonlyArray<T> {
161
151
  readonly length: int;
162
- readonly [n: number]: T;
163
152
  slice(start?: int, end?: int): T[];
164
153
  indexOf(searchElement: T, fromIndex?: int): int;
165
154
  lastIndexOf(searchElement: T, fromIndex?: int): int;
@@ -175,7 +164,6 @@ declare global {
175
164
  concat(...items: (T | readonly T[])[]): T[];
176
165
  join(separator?: string): string;
177
166
  at(index: int): T | undefined;
178
- [Symbol.iterator](): IterableIterator<T>;
179
167
  }
180
168
 
181
169
  interface ArrayConstructor {
@@ -189,7 +177,7 @@ declare global {
189
177
  const Array: ArrayConstructor;
190
178
 
191
179
  /**
192
- * String type with full JavaScript API
180
+ * String type - extends base with full JavaScript API
193
181
  */
194
182
  interface String {
195
183
  /**
@@ -322,7 +310,7 @@ declare global {
322
310
  const String: StringConstructor;
323
311
 
324
312
  /**
325
- * Number type
313
+ * Number type - extends base with methods
326
314
  */
327
315
  interface Number {
328
316
  toString(radix?: number): string;
@@ -352,8 +340,6 @@ declare global {
352
340
  /**
353
341
  * Boolean type
354
342
  */
355
- interface Boolean {}
356
-
357
343
  interface BooleanConstructor {
358
344
  new (value?: any): Boolean;
359
345
  (value?: any): boolean;
@@ -362,10 +348,9 @@ declare global {
362
348
  const Boolean: BooleanConstructor;
363
349
 
364
350
  /**
365
- * Object type
351
+ * Object type - extends base with methods
366
352
  */
367
353
  interface Object {
368
- constructor: Function;
369
354
  toString(): string;
370
355
  hasOwnProperty(v: PropertyKey): boolean;
371
356
  }
@@ -383,19 +368,15 @@ declare global {
383
368
  const Object: ObjectConstructor;
384
369
 
385
370
  /**
386
- * Function type
371
+ * Function type - extends base with methods
387
372
  */
388
373
  interface Function {
389
- prototype: any;
390
374
  readonly length: int;
391
375
  call(thisArg: any, ...argArray: any[]): any;
392
376
  apply(thisArg: any, argArray?: any): any;
393
377
  bind(thisArg: any, ...argArray: any[]): any;
394
378
  }
395
379
 
396
- interface CallableFunction extends Function {}
397
- interface NewableFunction extends Function {}
398
-
399
380
  interface FunctionConstructor {
400
381
  new (...args: string[]): Function;
401
382
  (...args: string[]): Function;
@@ -404,7 +385,7 @@ declare global {
404
385
  const Function: FunctionConstructor;
405
386
 
406
387
  /**
407
- * RegExp type
388
+ * RegExp type - extends base with full API
408
389
  */
409
390
  interface RegExp {
410
391
  exec(string: string): RegExpExecArray | null;
@@ -433,23 +414,6 @@ declare global {
433
414
  input?: string;
434
415
  }
435
416
 
436
- /**
437
- * Symbol type
438
- */
439
- interface SymbolConstructor {
440
- readonly iterator: symbol;
441
- readonly asyncIterator: symbol;
442
- readonly hasInstance: symbol;
443
- readonly isConcatSpreadable: symbol;
444
- readonly species: symbol;
445
- readonly toPrimitive: symbol;
446
- readonly toStringTag: symbol;
447
- }
448
-
449
- const Symbol: SymbolConstructor;
450
-
451
- type PropertyKey = string | number | symbol;
452
-
453
417
  /**
454
418
  * Math object
455
419
  */
@@ -577,129 +541,6 @@ declare global {
577
541
  function setInterval(callback: (...args: any[]) => void, ms?: number, ...args: any[]): number;
578
542
  function clearInterval(id: number | undefined): void;
579
543
 
580
- /**
581
- * Utility types (built into TypeScript)
582
- */
583
- type Partial<T> = { [P in keyof T]?: T[P] };
584
- type Required<T> = { [P in keyof T]-?: T[P] };
585
- type Readonly<T> = { readonly [P in keyof T]: T[P] };
586
- type Pick<T, K extends keyof T> = { [P in K]: T[P] };
587
- type Record<K extends keyof any, T> = { [P in K]: T };
588
- type Exclude<T, U> = T extends U ? never : T;
589
- type Extract<T, U> = T extends U ? T : never;
590
- type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
591
- type NonNullable<T> = T extends null | undefined ? never : T;
592
- type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
593
- type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;
594
- type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
595
- type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;
596
-
597
- /**
598
- * Promise type
599
- */
600
- interface Promise<T> {
601
- then<TResult1 = T, TResult2 = never>(
602
- onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
603
- onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
604
- ): Promise<TResult1 | TResult2>;
605
- catch<TResult = never>(
606
- onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null
607
- ): Promise<T | TResult>;
608
- finally(onfinally?: (() => void) | undefined | null): Promise<T>;
609
- }
610
-
611
- interface PromiseLike<T> {
612
- then<TResult1 = T, TResult2 = never>(
613
- onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,
614
- onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null
615
- ): PromiseLike<TResult1 | TResult2>;
616
- }
617
-
618
- interface PromiseConstructor {
619
- new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
620
- resolve(): Promise<void>;
621
- resolve<T>(value: T | PromiseLike<T>): Promise<T>;
622
- reject<T = never>(reason?: any): Promise<T>;
623
- all<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T[]>;
624
- race<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T>;
625
- }
626
-
627
- const Promise: PromiseConstructor;
628
-
629
- /**
630
- * Iterator types
631
- */
632
- interface Iterator<T, TReturn = any, TNext = undefined> {
633
- next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
634
- return?(value?: TReturn): IteratorResult<T, TReturn>;
635
- throw?(e?: any): IteratorResult<T, TReturn>;
636
- }
637
-
638
- interface IteratorResult<T, TReturn = any> {
639
- done: boolean;
640
- value: T | TReturn;
641
- }
642
-
643
- interface IteratorYieldResult<T> {
644
- done: false;
645
- value: T;
646
- }
647
-
648
- interface IteratorReturnResult<TReturn> {
649
- done: true;
650
- value: TReturn;
651
- }
652
-
653
- interface Iterable<T> {
654
- [Symbol.iterator](): Iterator<T>;
655
- }
656
-
657
- interface IterableIterator<T> extends Iterator<T> {
658
- [Symbol.iterator](): IterableIterator<T>;
659
- }
660
-
661
- /**
662
- * Async Iterator types (for for-await loops)
663
- */
664
- interface AsyncIterator<T, TReturn = any, TNext = undefined> {
665
- next(...args: [] | [TNext]): Promise<IteratorResult<T, TReturn>>;
666
- return?(value?: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T, TReturn>>;
667
- throw?(e?: any): Promise<IteratorResult<T, TReturn>>;
668
- }
669
-
670
- interface AsyncIterable<T> {
671
- [Symbol.asyncIterator](): AsyncIterator<T>;
672
- }
673
-
674
- interface AsyncIterableIterator<T> extends AsyncIterator<T> {
675
- [Symbol.asyncIterator](): AsyncIterableIterator<T>;
676
- }
677
-
678
- /**
679
- * Generator types
680
- */
681
- interface Generator<T = unknown, TReturn = any, TNext = unknown> extends Iterator<T, TReturn, TNext> {
682
- next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
683
- return(value: TReturn): IteratorResult<T, TReturn>;
684
- throw(e: any): IteratorResult<T, TReturn>;
685
- [Symbol.iterator](): Generator<T, TReturn, TNext>;
686
- }
687
-
688
- interface AsyncGenerator<T = unknown, TReturn = any, TNext = unknown> extends AsyncIterator<T, TReturn, TNext> {
689
- next(...args: [] | [TNext]): Promise<IteratorResult<T, TReturn>>;
690
- return(value: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T, TReturn>>;
691
- throw(e: any): Promise<IteratorResult<T, TReturn>>;
692
- [Symbol.asyncIterator](): AsyncGenerator<T, TReturn, TNext>;
693
- }
694
-
695
- /**
696
- * Template literal type utilities
697
- */
698
- type Uppercase<S extends string> = intrinsic;
699
- type Lowercase<S extends string> = intrinsic;
700
- type Capitalize<S extends string> = intrinsic;
701
- type Uncapitalize<S extends string> = intrinsic;
702
-
703
544
  /**
704
545
  * Additional types
705
546
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsonic/js-globals",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Global type definitions for Tsonic JS mode (noLib: true, JavaScript semantics)",
5
5
  "main": "index.d.ts",
6
6
  "types": "index.d.ts",