sveld 0.32.1 → 0.32.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 (3) hide show
  1. package/README.md +109 -0
  2. package/lib/index.js +587 -585
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -849,6 +849,80 @@ export type ComponentProps = {
849
849
 
850
850
  > **Note:** The inline syntax `@typedef {{ name: string }} User` continues to work for backwards compatibility.
851
851
 
852
+ #### Discriminated unions
853
+
854
+ A `@typedef` can be a union of object literals, optionally mixed with primitive members. `sveld` emits these as `export type X = ...` aliases (not `interface`), so the discriminant narrows correctly on the consumer side.
855
+
856
+ **Signature:**
857
+
858
+ ```js
859
+ /**
860
+ * @typedef {A | B | C} TypeName
861
+ */
862
+ ```
863
+
864
+ **Example:**
865
+
866
+ ```svelte
867
+ <script>
868
+ /**
869
+ * @typedef {{ kind: "success"; value: string } | { kind: "error"; error: Error }} Result
870
+ * @typedef {{ ok: true; data: number } | { ok: false; reason: string } | "pending"} Status
871
+ */
872
+
873
+ /** @type {Result} */
874
+ export let result = { kind: "success", value: "ok" };
875
+
876
+ /** @type {Status} */
877
+ export let status = "pending";
878
+ </script>
879
+ ```
880
+
881
+ Output:
882
+
883
+ ```ts
884
+ export type Result = { kind: "success"; value: string } | { kind: "error"; error: Error };
885
+
886
+ export type Status = { ok: true; data: number } | { ok: false; reason: string } | "pending";
887
+
888
+ export type ComponentProps = {
889
+ /** @default { kind: "success", value: "ok" } */
890
+ result?: Result;
891
+ /** @default "pending" */
892
+ status?: Status;
893
+ };
894
+ ```
895
+
896
+ Consumers can then narrow on the discriminant:
897
+
898
+ ```ts
899
+ function describe(r: Result) {
900
+ switch (r.kind) {
901
+ case "success":
902
+ return r.value;
903
+ case "error":
904
+ return r.error.message;
905
+ }
906
+ }
907
+ ```
908
+
909
+ The same pattern works inline via `@type`, which is useful when the union is only used for a single prop:
910
+
911
+ ```js
912
+ /** @type {{ kind: "success"; value: string } | { kind: "error"; error: Error }} */
913
+ export let result = { kind: "success", value: "ok" };
914
+ ```
915
+
916
+ In `<script lang="ts">` components, write the type alias directly — `sveld` preserves it in the emitted `.d.ts`:
917
+
918
+ ```svelte
919
+ <script lang="ts">
920
+ type Result = { kind: "success"; value: string } | { kind: "error"; error: Error };
921
+
922
+ let { result = { kind: "success", value: "ok" } }: { result?: Result } = $props();
923
+ </script>
924
+ ```
925
+
852
926
  ### `@callback`
853
927
 
854
928
  The `@callback` tag defines a function type using `@param` and `@returns` tags, following the [TypeScript JSDoc `@callback` specification](https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html#callback). Like `@typedef`, callbacks are exported from the generated TypeScript definition file.
@@ -1410,6 +1484,41 @@ export default class Component extends SvelteComponentTyped<
1410
1484
  > {}
1411
1485
  ```
1412
1486
 
1487
+ #### Discriminated unions in event details
1488
+
1489
+ When the event detail is a union (or any non-object shape), use `@type` to declare it directly. An explicit `@type` takes precedence over `@property` tags, so the union is preserved verbatim in the emitted `.d.ts` rather than being flattened into independent property unions. The special form `@type {object}` is the only exception — it signals that the shape should be built from the `@property` tags (as shown above).
1490
+
1491
+ **Example:**
1492
+
1493
+ ```svelte
1494
+ <script>
1495
+ /**
1496
+ * @event sort
1497
+ * @type {{ key: null; direction: "none" } | { key: string; direction: "ascending" | "descending" }}
1498
+ * Dispatched when a sortable column header would change the active sort.
1499
+ */
1500
+
1501
+ import { createEventDispatcher } from "svelte";
1502
+
1503
+ const dispatch = createEventDispatcher();
1504
+ </script>
1505
+ ```
1506
+
1507
+ Output:
1508
+
1509
+ ```ts
1510
+ export default class Component extends SvelteComponentTyped<
1511
+ ComponentProps,
1512
+ {
1513
+ /** Dispatched when a sortable column header would change the active sort. */
1514
+ sort: CustomEvent<{ key: null; direction: "none" } | { key: string; direction: "ascending" | "descending" }>;
1515
+ },
1516
+ Record<string, never>
1517
+ > {}
1518
+ ```
1519
+
1520
+ Any free-text prose after the tags is attached to the event description, not to a property doc.
1521
+
1413
1522
  ### Context API
1414
1523
 
1415
1524
  `sveld` automatically generates TypeScript definitions for Svelte's `setContext`/`getContext` API by extracting types from JSDoc annotations on the context values.