sveld 0.32.0 → 0.32.2

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/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.