@tsonic/core 0.4.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/types.d.ts +33 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsonic/core",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Core type definitions for Tsonic - TypeScript to native compiler",
5
5
  "type": "module",
6
6
  "keywords": [
package/types.d.ts CHANGED
@@ -141,3 +141,36 @@ export type inref<T> = T;
141
141
  export interface struct {
142
142
  readonly __brand: unique symbol;
143
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;