Repository navigation
Expand file tree
/
Copy pathsvelte-shims.d.ts
More file actions
123 lines (111 loc) · 3.84 KB
/
Copy pathsvelte-shims.d.ts
File metadata and controls
123 lines (111 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import 'svelte2tsx/svelte-shims-v4';
import type { JSX } from 'astro/jsx-runtime';
export type AstroClientDirectives = JSX.AstroComponentDirectives;
type Snippet = import('svelte').Snippet;
/**
* Helper to detect index-signature-like keys.
*/
type IsIndexSignatureKey<K> = string extends K
? true
: number extends K
? true
: symbol extends K
? true
: false;
/**
* Removes index signatures whose value type is `never`.
* (Keeps normal, explicitly-declared keys unchanged.)
*/
export type StripNeverIndexSignatures<T> = {
[K in keyof T as IsIndexSignatureKey<K> extends true
? [T[K]] extends [never]
? never
: K
: K]: T[K];
};
/**
* If `children` exists and is `never`, make it `{ children?: undefined }`
* (works even when it was required).
* If `children` doesn't exist, add `{ children?: undefined }`.
*/
type NormalizeNeverChildren<T> = 'children' extends keyof T
? [T['children']] extends [never]
? Omit<T, 'children'> & { children?: undefined }
: T
: T & { children?: undefined };
/**
* If `children` includes `Snippet` (even as part of a union), widen to `any`
* to allow arbitrary content.
*/
type WidenChildrenIfSnippet<T> = {
[K in keyof T]: K extends 'children'
? Extract<NonNullable<T[K]>, Snippet> extends never
? T[K]
: any
: T[K];
};
/**
* Detect `any` without triggering on generic type parameters.
* `0 extends 1 & T` is `true` only when `T` is `any`.
*/
type IsAny<T> = 0 extends 1 & T ? true : false;
/**
* Check whether a prop type is `Snippet`-like or `any` (which subsumes `Snippet`).
* Returns `true` for `any`, `Snippet`, `Snippet<[...]>`, `Snippet | null`, etc.
* Returns `false` for concrete non-snippet types and deferred generic parameters.
*/
type IsSnippetOrAny<T> =
IsAny<T> extends true ? true : Extract<NonNullable<T>, Snippet> extends never ? false : true;
/**
* Handle Snippet-typed and `any`-typed props for Astro compatibility:
* - `children` that includes `Snippet`: widen to `any` (Astro slot/content)
* - Other props whose type is `any` or includes `Snippet`: make optional.
* `svelte2tsx` types renamed/destructured snippet props and event handlers
* as `any` without marking them optional, even when they have defaults.
* Astro cannot pass Svelte snippets, so these are safe to omit.
*
* NOTE: This type uses an intersection of two mapped types, which breaks
* TypeScript's generic type parameter inference. Use `GenericPropsWithClientDirectives`
* for generic Svelte components instead.
*/
type HandleSnippetProps<T> = {
[K in keyof T as K extends 'children'
? K
: IsSnippetOrAny<T[K]> extends true
? never
: K]: K extends 'children'
? Extract<NonNullable<T[K]>, Snippet> extends never
? T[K]
: any
: T[K];
} & {
[K in keyof T as K extends 'children'
? never
: IsSnippetOrAny<T[K]> extends true
? K
: never]?: T[K];
};
/**
* `T` (Svelte props) made safe for Astro:
* - Normalize `children` (avoid `never`/missing cases)
* - Widen snippet-based `children` to `any` (Astro slot/content compatibility)
* - Make snippet/any-typed non-children props optional
* - Strip useless `never` index signatures (allow extra keys like `client:*`)
* - Add Astro client directives
*/
export type PropsWithClientDirectives<T> = StripNeverIndexSignatures<
HandleSnippetProps<NormalizeNeverChildren<T>>
> &
AstroClientDirectives;
/**
* `T` (Svelte props) made safe for Astro (generic components):
* Uses a simpler homomorphic mapped type that preserves generic inference.
* - Normalize `children` (avoid `never`/missing cases)
* - Widen snippet-based `children` to `any` (Astro slot/content compatibility)
* - Strip useless `never` index signatures (allow extra keys like `client:*`)
* - Add Astro client directives
*/
export type GenericPropsWithClientDirectives<T> = StripNeverIndexSignatures<
WidenChildrenIfSnippet<NormalizeNeverChildren<T>>
> &
AstroClientDirectives;