Add TS-able snippets to docs examples

pull/8452/head
Puru Vijay 3 years ago
parent 7131650631
commit 9ce5d9636d

@ -69,6 +69,7 @@ You can use reserved words as prop names.
```svelte ```svelte
<script> <script>
/** @type {string} */
let className; let className;
// creates a `class` property, even // creates a `class` property, even
@ -192,6 +193,7 @@ If a statement consists entirely of an assignment to an undeclared variable, Sve
```svelte ```svelte
<script> <script>
/** @type {number} */
export let num; export let num;
// we don't need to declare `squared` and `cubed` // we don't need to declare `squared` and `cubed`

@ -276,6 +276,7 @@ Inputs that work together can use `bind:group`.
```svelte ```svelte
<script> <script>
let tortilla = 'Plain'; let tortilla = 'Plain';
/** @type {Array<string>} */
let fillings = []; let fillings = [];
</script> </script>
@ -303,6 +304,7 @@ To get a reference to a DOM node, use `bind:this`.
<script> <script>
import { onMount } from 'svelte'; import { onMount } from 'svelte';
/** @type {HTMLCanvasElement} */
let canvasElement; let canvasElement;
onMount(() => { onMount(() => {
@ -399,6 +401,7 @@ Actions are functions that are called when an element is created. They can retur
```svelte ```svelte
<script> <script>
/** @param {HTMLElement} node */
function foo(node) { function foo(node) {
// the node has been mounted in the DOM // the node has been mounted in the DOM
@ -505,6 +508,7 @@ The function is called repeatedly _before_ the transition begins, with different
<script> <script>
import { elasticOut } from 'svelte/easing'; import { elasticOut } from 'svelte/easing';
/** @type {boolean} */
export let visible; export let visible;
function whoosh(node, params) { function whoosh(node, params) {
@ -710,10 +714,17 @@ The `t` argument passed to `css` is a value that goes from `0` and `1` after the
The function is called repeatedly _before_ the animation begins, with different `t` and `u` arguments. The function is called repeatedly _before_ the animation begins, with different `t` and `u` arguments.
<!-- TODO: Types -->
```svelte ```svelte
<script> <script>
import { cubicOut } from 'svelte/easing'; import { cubicOut } from 'svelte/easing';
/**
* @param {HTMLElement} node
* @param {{ from: DOMRect, to: DOMRect }} states
* @param {any} params
*/
function whizz(node, { from, to }, params) { function whizz(node, { from, to }, params) {
const dx = from.left - to.left; const dx = from.left - to.left;
const dy = from.top - to.top; const dy = from.top - to.top;
@ -742,6 +753,11 @@ A custom animation function can also return a `tick` function, which is called _
<script> <script>
import { cubicOut } from 'svelte/easing'; import { cubicOut } from 'svelte/easing';
/**
* @param {HTMLElement} node
* @param {{ from: DOMRect, to: DOMRect }} states
* @param {any} params
*/
function whizz(node, { from, to }, params) { function whizz(node, { from, to }, params) {
const dx = from.left - to.left; const dx = from.left - to.left;
const dy = from.top - to.top; const dy = from.top - to.top;

@ -156,6 +156,7 @@ It cannot appear at the top level of your markup; it must be inside an if or eac
```svelte ```svelte
<script> <script>
/** @type {number} */
export let count; export let count;
</script> </script>
@ -198,6 +199,8 @@ If `this` is the name of a [void element](https://developer.mozilla.org/en-US/do
```svelte ```svelte
<script> <script>
let tag = 'div'; let tag = 'div';
/** @type {(e: MouseEvent) => void} */
export let handler; export let handler;
</script> </script>
@ -220,6 +223,7 @@ Unlike `<svelte:self>`, this element may only appear at the top level of your co
```svelte ```svelte
<script> <script>
/** @param {KeyboardEvent} event */
function handleKeydown(event) { function handleKeydown(event) {
alert(`pressed the ${event.key} key`); alert(`pressed the ${event.key} key`);
} }

@ -109,7 +109,8 @@ Out of `onMount`, `beforeUpdate`, `afterUpdate` and `onDestroy`, this is the onl
## `tick` ## `tick`
```js ```js
promise: Promise = tick(); /** @type {Promise<void>} */
promise = tick();
``` ```
Returns a promise that resolves once any pending state changes have been applied, or in the next microtask if there are none. Returns a promise that resolves once any pending state changes have been applied, or in the next microtask if there are none.
@ -128,6 +129,8 @@ Returns a promise that resolves once any pending state changes have been applied
## `setContext` ## `setContext`
<!-- TODO: Better typing information -->
```js ```js
setContext(key: any, context: any) setContext(key: any, context: any)
``` ```
@ -165,7 +168,7 @@ Retrieves the context that belongs to the closest parent component with the spec
## `hasContext` ## `hasContext`
```js ```js
hasContext: boolean = hasContext(key: any) hasContext = hasContext(key: any)
``` ```
Checks whether a given `key` has been set in the context of a parent component. Must be called during component initialisation. Checks whether a given `key` has been set in the context of a parent component. Must be called during component initialisation.

@ -70,6 +70,7 @@ Creates a store whose value cannot be set from 'outside', the first argument is
```js ```js
import { readable } from 'svelte/store'; import { readable } from 'svelte/store';
/** @type {import('svelte/store').Readable<Date>} */
const time = readable(null, (set) => { const time = readable(null, (set) => {
set(new Date()); set(new Date());

@ -172,10 +172,11 @@ A number of [community-maintained preprocessing plugins](https://sveltesociety.d
You can write your own preprocessor using the `svelte.preprocess` API. You can write your own preprocessor using the `svelte.preprocess` API.
```js ```js
result: { /** @type {{
code: string, code: string,
dependencies: Array<string> dependencies: Array<string>
} = await svelte.preprocess( }} */
result = await svelte.preprocess(
source: string, source: string,
preprocessors: Array<{ preprocessors: Array<{
markup?: (input: { content: string, filename: string }) => Promise<{ markup?: (input: { content: string, filename: string }) => Promise<{

Loading…
Cancel
Save