@tsonic/core 0.3.0 → 0.5.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.
- package/package.json +1 -1
- package/types.d.ts +81 -3
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -52,9 +52,54 @@ export type char = string; // System.Char (single UTF-16 code unit)
|
|
|
52
52
|
// Erases to unknown for type safety - requires explicit handling
|
|
53
53
|
export type ptr<T> = unknown;
|
|
54
54
|
|
|
55
|
-
//
|
|
56
|
-
// Parameter
|
|
57
|
-
//
|
|
55
|
+
// ============================================================================
|
|
56
|
+
// Parameter Passing Modifiers
|
|
57
|
+
// ============================================================================
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Marks a parameter as `out` - callee sets the value.
|
|
61
|
+
* Use with `as out<T>` at call sites.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* import { int, out } from "@tsonic/core/types.js";
|
|
66
|
+
*
|
|
67
|
+
* let value: int = 0;
|
|
68
|
+
* dict.tryGetValue("key", value as out<int>);
|
|
69
|
+
* // value now contains the result
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export type out<T> = T;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Marks a parameter as `ref` - callee can read and modify.
|
|
76
|
+
* Use with `as ref<T>` at call sites.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* import { int, ref } from "@tsonic/core/types.js";
|
|
81
|
+
*
|
|
82
|
+
* let count: int = 10;
|
|
83
|
+
* increment(count as ref<int>);
|
|
84
|
+
* // count is now modified
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export type ref<T> = T;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Marks a parameter as `in` - read-only reference (optimization for large structs).
|
|
91
|
+
* Use with `as inref<T>` at call sites.
|
|
92
|
+
* Named `inref` because `in` is a TypeScript reserved keyword.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* import { inref } from "@tsonic/core/types.js";
|
|
97
|
+
*
|
|
98
|
+
* const data: LargeStruct = { ... };
|
|
99
|
+
* process(data as inref<LargeStruct>);
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export type inref<T> = T;
|
|
58
103
|
|
|
59
104
|
// ============================================================================
|
|
60
105
|
// Value Type Markers
|
|
@@ -96,3 +141,36 @@ export type ptr<T> = unknown;
|
|
|
96
141
|
export interface struct {
|
|
97
142
|
readonly __brand: unique symbol;
|
|
98
143
|
}
|
|
144
|
+
|
|
145
|
+
// ============================================================================
|
|
146
|
+
// Safe Casting
|
|
147
|
+
// ============================================================================
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Safe cast function - returns T or null if cast fails.
|
|
151
|
+
*
|
|
152
|
+
* TypeScript: `tryCast<Person>(obj)`
|
|
153
|
+
* C#: `obj as Person`
|
|
154
|
+
*
|
|
155
|
+
* Unlike `x as T` which throws on failure (C# `(T)x`), this returns null.
|
|
156
|
+
* This is the C# "as" operator behavior.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* import { tryCast } from "@tsonic/core/types.js";
|
|
161
|
+
*
|
|
162
|
+
* const person = tryCast<Person>(unknownObj);
|
|
163
|
+
* if (person !== null) {
|
|
164
|
+
* console.log(person.name);
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* In C#, this emits:
|
|
169
|
+
* ```csharp
|
|
170
|
+
* Person? person = unknownObj as Person;
|
|
171
|
+
* if (person != null) {
|
|
172
|
+
* Console.WriteLine(person.name);
|
|
173
|
+
* }
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export declare function tryCast<T>(value: unknown): T | null;
|