Skip to content

Commit 9bee59c

Browse files
PuruVJdummdidumm
andauthored
feat(site-2): TS in docs (#8452)
* Commit * Push working type generation code * Fix type gen script invocation * Delete type-info.js * Change build script * Recreate package lock * mkdir generated * Add type references to some pages * Add TS-able snippets to docs examples * Fix some stuff * Add types to individual functions * Add to store page * Refactor compile-types.js * Move ts sourcefile stuff to compile-types * Remove commented code * Half attempt at export aliases * Add * Fix, add disclaimer for svelte/internal * Disable /docs prerendering * Remove internals page * Remove redundant stuff * Make search work with it * Fix compile types logic * Add file: comment, * Add two-slash to docs pages * Get type links working * Remove console.log * Add action module * Fix actions logic * Make compile-types logic more robust * Bump site-kit * Fix gitignore * Use moduleResolution bundler * Don't apply shiki-twoslash to auto generated code snippets from render * Recreate package lock * Make TSbpage undraft * Fix svelte component types * Remove console.log * No more sveltekit * Make regex smarter * Update sites/svelte.dev/scripts/type-gen/compile-types.js Co-authored-by: Simon H <[email protected]> * Rebuild package lock * Remove commented out code * Update deps * Remove $$_attributes --------- Co-authored-by: Simon H <[email protected]>
1 parent b9ea60c commit 9bee59c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2021
-584
lines changed

site/content/docs/02-template-syntax/01-svelte-components.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ In development mode (see the [compiler options](/docs/svelte-compiler#svelte-com
5252
If you export a `const`, `class` or `function`, it is readonly from outside the component. Functions are valid prop values, however, as shown below.
5353

5454
```svelte
55+
<!--- file: App.svelte --->
5556
<script>
5657
// these are readonly
5758
export const thisIs = 'readonly';
5859
60+
/** @param {string} name */
5961
export function greet(name) {
6062
alert(`hello ${name}!`);
6163
}
@@ -70,7 +72,9 @@ Readonly props can be accessed as properties on the element, tied to the compone
7072
You can use reserved words as prop names.
7173

7274
```svelte
75+
<!--- file: App.svelte --->
7376
<script>
77+
/** @type {string} */
7478
let className;
7579
7680
// creates a `class` property, even
@@ -155,10 +159,12 @@ Any top-level statement (i.e. not inside a block or a function) can be made reac
155159
Only values which directly appear within the `$:` block will become dependencies of the reactive statement. For example, in the code below `total` will only update when `x` changes, but not `y`.
156160

157161
```svelte
162+
<!--- file: App.svelte --->
158163
<script>
159164
let x = 0;
160165
let y = 0;
161166
167+
/** @param {number} value */
162168
function yPlusAValue(value) {
163169
return value + y;
164170
}
@@ -179,9 +185,10 @@ It is important to note that the reactive blocks are ordered via simple static a
179185
let x = 0;
180186
let y = 0;
181187
182-
const setY = (value) => {
188+
/** @param {number} value */
189+
function setY(value) {
183190
y = value;
184-
};
191+
}
185192
186193
$: yDependent = y;
187194
$: setY(x);
@@ -193,7 +200,9 @@ Moving the line `$: yDependent = y` below `$: setY(x)` will cause `yDependent` t
193200
If a statement consists entirely of an assignment to an undeclared variable, Svelte will inject a `let` declaration on your behalf.
194201

195202
```svelte
203+
<!--- file: App.svelte --->
196204
<script>
205+
/** @type {number} */
197206
export let num;
198207
199208
// we don't need to declare `squared` and `cubed`
@@ -232,7 +241,8 @@ Local variables (that do not represent store values) must _not_ have a `$` prefi
232241

233242
#### Store contract
234243

235-
```js
244+
```ts
245+
// @noErrors
236246
store = { subscribe: (subscription: (value: any) => void) => (() => void), set?: (value: any) => void }
237247
```
238248

site/content/docs/02-template-syntax/05-element-directives.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ on:eventname|modifiers={handler}
1717
Use the `on:` directive to listen to DOM events.
1818

1919
```svelte
20+
<!--- file: App.svelte --->
2021
<script>
2122
let count = 0;
2223
24+
/** @param {MouseEvent} event */
2325
function handleClick(event) {
2426
count += 1;
2527
}
@@ -70,12 +72,14 @@ If the `on:` directive is used without a value, the component will _forward_ the
7072
It's possible to have multiple event listeners for the same event:
7173

7274
```svelte
75+
<!--- file: App.svelte --->
7376
<script>
7477
let counter = 0;
7578
function increment() {
7679
counter = counter + 1;
7780
}
7881
82+
/** @param {MouseEvent} event */
7983
function track(event) {
8084
trackEvent(event);
8185
}
@@ -274,8 +278,11 @@ bind:group={variable}
274278
Inputs that work together can use `bind:group`.
275279

276280
```svelte
281+
<!--- file: App.svelte --->
277282
<script>
278283
let tortilla = 'Plain';
284+
285+
/** @type {Array<string>} */
279286
let fillings = [];
280287
</script>
281288
@@ -302,9 +309,11 @@ bind:this={dom_node}
302309
To get a reference to a DOM node, use `bind:this`.
303310

304311
```svelte
312+
<!--- file: App.svelte --->
305313
<script>
306314
import { onMount } from 'svelte';
307315
316+
/** @type {HTMLCanvasElement} */
308317
let canvasElement;
309318
310319
onMount(() => {
@@ -390,7 +399,8 @@ use:action
390399
use:action={parameters}
391400
```
392401

393-
```js
402+
```ts
403+
// @noErrors
394404
action = (node: HTMLElement, parameters: any) => {
395405
update?: (parameters: any) => void,
396406
destroy?: () => void
@@ -400,7 +410,9 @@ action = (node: HTMLElement, parameters: any) => {
400410
Actions are functions that are called when an element is created. They can return an object with a `destroy` method that is called after the element is unmounted:
401411

402412
```svelte
413+
<!--- file: App.svelte --->
403414
<script>
415+
/** @type {import('svelte/action').Action} */
404416
function foo(node) {
405417
// the node has been mounted in the DOM
406418
@@ -420,9 +432,11 @@ An action can have a parameter. If the returned value has an `update` method, it
420432
> Don't worry about the fact that we're redeclaring the `foo` function for every component instance — Svelte will hoist any functions that don't depend on local state out of the component definition.
421433
422434
```svelte
435+
<!--- file: App.svelte --->
423436
<script>
424437
export let bar;
425438
439+
/** @type {import('svelte/action').Action} */
426440
function foo(node, bar) {
427441
// the node has been mounted in the DOM
428442
@@ -460,6 +474,7 @@ transition:fn|local={params}
460474
```
461475

462476
```js
477+
// @noErrors
463478
transition = (node: HTMLElement, params: any, options: { direction: 'in' | 'out' | 'both' }) => {
464479
delay?: number,
465480
duration?: number,
@@ -504,9 +519,11 @@ The `t` argument passed to `css` is a value between `0` and `1` after the `easin
504519
The function is called repeatedly _before_ the transition begins, with different `t` and `u` arguments.
505520

506521
```svelte
522+
<!--- file: App.svelte --->
507523
<script>
508524
import { elasticOut } from 'svelte/easing';
509525
526+
/** @type {boolean} */
510527
export let visible;
511528
512529
function whoosh(node, params) {
@@ -657,6 +674,7 @@ animate:name={params}
657674
```
658675

659676
```js
677+
// @noErrors
660678
animation = (node: HTMLElement, { from: DOMRect, to: DOMRect } , params: any) => {
661679
delay?: number,
662680
duration?: number,
@@ -667,6 +685,7 @@ animation = (node: HTMLElement, { from: DOMRect, to: DOMRect } , params: any) =>
667685
```
668686

669687
```ts
688+
// @noErrors
670689
DOMRect {
671690
bottom: number,
672691
height: number,
@@ -712,10 +731,20 @@ The `t` argument passed to `css` is a value that goes from `0` and `1` after the
712731

713732
The function is called repeatedly _before_ the animation begins, with different `t` and `u` arguments.
714733

734+
<!-- TODO: Types -->
735+
715736
```svelte
737+
<!--- file: App.svelte --->
716738
<script>
717739
import { cubicOut } from 'svelte/easing';
718740
741+
/**
742+
* @param {HTMLElement} node
743+
* @param {Object} states
744+
* @param {DOMRect} states.from
745+
* @param {DOMRect} states.to
746+
* @param {any} params
747+
*/
719748
function whizz(node, { from, to }, params) {
720749
const dx = from.left - to.left;
721750
const dy = from.top - to.top;
@@ -744,6 +773,13 @@ A custom animation function can also return a `tick` function, which is called _
744773
<script>
745774
import { cubicOut } from 'svelte/easing';
746775
776+
/**
777+
* @param {HTMLElement} node
778+
* @param {Object} states
779+
* @param {DOMRect} states.from
780+
* @param {DOMRect} states.to
781+
* @param {any} params
782+
*/
747783
function whizz(node, { from, to }, params) {
748784
const dx = from.left - to.left;
749785
const dy = from.top - to.top;

site/content/docs/02-template-syntax/07-special-elements.md

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ It cannot appear at the top level of your markup; it must be inside an if or eac
156156

157157
```svelte
158158
<script>
159+
/** @type {number} */
159160
export let count;
160161
</script>
161162
@@ -198,6 +199,8 @@ If `this` is the name of a [void element](https://developer.mozilla.org/en-US/do
198199
```svelte
199200
<script>
200201
let tag = 'div';
202+
203+
/** @type {(e: MouseEvent) => void} */
201204
export let handler;
202205
</script>
203206
@@ -220,6 +223,7 @@ Unlike `<svelte:self>`, this element may only appear at the top level of your co
220223

221224
```svelte
222225
<script>
226+
/** @param {KeyboardEvent} event */
223227
function handleKeydown(event) {
224228
alert(`pressed the ${event.key} key`);
225229
}

site/content/docs/03-runtime/01-svelte.md

+14-34
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ The `svelte` package exposes [lifecycle functions](/tutorial/onmount) and the [c
66

77
## `onMount`
88

9-
```js
10-
onMount(callback: () => void)
11-
```
12-
13-
```js
14-
onMount(callback: () => () => void)
15-
```
9+
> EXPORT_SNIPPET: svelte#onMount
1610
1711
The `onMount` function schedules a callback to run as soon as the component has been mounted to the DOM. It must be called during the component's initialisation (but doesn't need to live _inside_ the component; it can be called from an external module).
1812

@@ -48,9 +42,7 @@ If a function is returned from `onMount`, it will be called when the component i
4842
4943
## `beforeUpdate`
5044

51-
```js
52-
beforeUpdate(callback: () => void)
53-
```
45+
> EXPORT_SNIPPET: svelte#beforeUpdate
5446
5547
Schedules a callback to run immediately before the component is updated after any state change.
5648

@@ -68,9 +60,7 @@ Schedules a callback to run immediately before the component is updated after an
6860

6961
## `afterUpdate`
7062

71-
```js
72-
afterUpdate(callback: () => void)
73-
```
63+
> EXPORT_SNIPPET: svelte#afterUpdate
7464
7565
Schedules a callback to run immediately after the component has been updated.
7666

@@ -88,9 +78,7 @@ Schedules a callback to run immediately after the component has been updated.
8878

8979
## `onDestroy`
9080

91-
```js
92-
onDestroy(callback: () => void)
93-
```
81+
> EXPORT_SNIPPET: svelte#onDestroy
9482
9583
Schedules a callback to run immediately before the component is unmounted.
9684

@@ -108,9 +96,7 @@ Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the onl
10896

10997
## `tick`
11098

111-
```js
112-
promise: Promise = tick();
113-
```
99+
> EXPORT_SNIPPET: svelte#tick
114100
115101
Returns a promise that resolves once any pending state changes have been applied, or in the next microtask if there are none.
116102

@@ -128,9 +114,7 @@ Returns a promise that resolves once any pending state changes have been applied
128114

129115
## `setContext`
130116

131-
```js
132-
setContext(key: any, context: any)
133-
```
117+
> EXPORT_SNIPPET: svelte#setContext
134118
135119
Associates an arbitrary `context` object with the current component and the specified `key` and returns that object. The context is then available to children of the component (including slotted content) with `getContext`.
136120

@@ -148,9 +132,7 @@ Like lifecycle functions, this must be called during component initialisation.
148132
149133
## `getContext`
150134

151-
```js
152-
context: any = getContext(key: any)
153-
```
135+
> EXPORT_SNIPPET: svelte#getContext
154136
155137
Retrieves the context that belongs to the closest parent component with the specified `key`. Must be called during component initialisation.
156138

@@ -164,9 +146,7 @@ Retrieves the context that belongs to the closest parent component with the spec
164146

165147
## `hasContext`
166148

167-
```js
168-
hasContext: boolean = hasContext(key: any)
169-
```
149+
> EXPORT_SNIPPET: svelte#hasContext
170150
171151
Checks whether a given `key` has been set in the context of a parent component. Must be called during component initialisation.
172152

@@ -182,9 +162,7 @@ Checks whether a given `key` has been set in the context of a parent component.
182162

183163
## `getAllContexts`
184164

185-
```js
186-
contexts: Map<any, any> = getAllContexts()
187-
```
165+
> EXPORT_SNIPPET: svelte#getAllContexts
188166
189167
Retrieves the whole context map that belongs to the closest parent component. Must be called during component initialisation. Useful, for example, if you programmatically create a component and want to pass the existing context to it.
190168

@@ -198,9 +176,7 @@ Retrieves the whole context map that belongs to the closest parent component. Mu
198176

199177
## `createEventDispatcher`
200178

201-
```js
202-
dispatch: ((name: string, detail?: any, options?: DispatchOptions) => boolean) = createEventDispatcher();
203-
```
179+
> EXPORT_SNIPPET: svelte#createEventDispatcher
204180
205181
Creates an event dispatcher that can be used to dispatch [component events](/docs/component-directives#on-eventname). Event dispatchers are functions that can take two arguments: `name` and `detail`.
206182

@@ -246,3 +222,7 @@ Events can be cancelable by passing a third parameter to the dispatch function.
246222
}
247223
</script>
248224
```
225+
226+
## Types
227+
228+
> TYPES: svelte

0 commit comments

Comments
 (0)