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 +74 -0
- package/lib/index.js +642 -642
- package/package.json +2 -2
- /package/lib/{src/ComponentParser.d.ts → ComponentParser.d.ts} +0 -0
- /package/lib/{src/cli.d.ts → cli.d.ts} +0 -0
- /package/lib/{src/create-exports.d.ts → create-exports.d.ts} +0 -0
- /package/lib/{src/element-tag-map.d.ts → element-tag-map.d.ts} +0 -0
- /package/lib/{src/get-svelte-entry.d.ts → get-svelte-entry.d.ts} +0 -0
- /package/lib/{src/index.d.ts → index.d.ts} +0 -0
- /package/lib/{src/parse-exports.d.ts → parse-exports.d.ts} +0 -0
- /package/lib/{src/path.d.ts → path.d.ts} +0 -0
- /package/lib/{src/plugin.d.ts → plugin.d.ts} +0 -0
- /package/lib/{src/resolve-alias.d.ts → resolve-alias.d.ts} +0 -0
- /package/lib/{src/sveld.d.ts → sveld.d.ts} +0 -0
- /package/lib/{src/writer → writer}/MarkdownWriterBase.d.ts +0 -0
- /package/lib/{src/writer → writer}/Writer.d.ts +0 -0
- /package/lib/{src/writer → writer}/WriterMarkdown.d.ts +0 -0
- /package/lib/{src/writer → writer}/markdown-format-utils.d.ts +0 -0
- /package/lib/{src/writer → writer}/markdown-render-utils.d.ts +0 -0
- /package/lib/{src/writer → writer}/writer-json.d.ts +0 -0
- /package/lib/{src/writer → writer}/writer-markdown-core.d.ts +0 -0
- /package/lib/{src/writer → writer}/writer-markdown.d.ts +0 -0
- /package/lib/{src/writer → writer}/writer-ts-definitions-core.d.ts +0 -0
- /package/lib/{src/writer → writer}/writer-ts-definitions.d.ts +0 -0
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.
|