-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(svelte5): update typings to support new component types (#400)
- Loading branch information
Showing
11 changed files
with
154 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,4 @@ | |
}) | ||
</script> | ||
<button></button> | ||
<button>click me</button> |
2 changes: 2 additions & 0 deletions
2
src/__tests__/fixtures/Simple.svelte → src/__tests__/fixtures/Typed.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script lang="ts"> | ||
const { name, count }: { name: string; count: number } = $props() | ||
export const hello: string = 'hello' | ||
</script> | ||
|
||
<h1>hello {name}</h1> | ||
<p>count: {count}</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expectTypeOf } from 'expect-type' | ||
import { describe, test } from 'vitest' | ||
|
||
import * as subject from '../index.js' | ||
import Component from './fixtures/TypedRunes.svelte' | ||
|
||
describe('types', () => { | ||
test('render is a function that accepts a Svelte component', () => { | ||
subject.render(Component, { name: 'Alice', count: 42 }) | ||
subject.render(Component, { props: { name: 'Alice', count: 42 } }) | ||
}) | ||
|
||
test('rerender is a function that accepts partial props', async () => { | ||
const { rerender } = subject.render(Component, { name: 'Alice', count: 42 }) | ||
|
||
await rerender({ name: 'Bob' }) | ||
await rerender({ count: 0 }) | ||
}) | ||
|
||
test('invalid prop types are rejected', () => { | ||
// @ts-expect-error: name should be a string | ||
subject.render(Component, { name: 42 }) | ||
|
||
// @ts-expect-error: name should be a string | ||
subject.render(Component, { props: { name: 42 } }) | ||
}) | ||
|
||
test('render result has container and component', () => { | ||
const result = subject.render(Component, { name: 'Alice', count: 42 }) | ||
|
||
expectTypeOf(result).toMatchTypeOf<{ | ||
container: HTMLElement | ||
component: { hello: string } | ||
debug: (el?: HTMLElement) => void | ||
rerender: (props: { name?: string; count?: number }) => Promise<void> | ||
unmount: () => void | ||
}>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { expectTypeOf } from 'expect-type' | ||
import { describe, test } from 'vitest' | ||
|
||
import * as subject from '../index.js' | ||
import Component from './fixtures/Typed.svelte' | ||
|
||
describe('types', () => { | ||
test('render is a function that accepts a Svelte component', () => { | ||
subject.render(Component, { name: 'Alice', count: 42 }) | ||
subject.render(Component, { props: { name: 'Alice', count: 42 } }) | ||
}) | ||
|
||
test('rerender is a function that accepts partial props', async () => { | ||
const { rerender } = subject.render(Component, { name: 'Alice', count: 42 }) | ||
|
||
await rerender({ name: 'Bob' }) | ||
await rerender({ count: 0 }) | ||
}) | ||
|
||
test('invalid prop types are rejected', () => { | ||
// @ts-expect-error: name should be a string | ||
subject.render(Component, { name: 42 }) | ||
|
||
// @ts-expect-error: name should be a string | ||
subject.render(Component, { props: { name: 42 } }) | ||
}) | ||
|
||
test('render result has container and component', () => { | ||
const result = subject.render(Component, { name: 'Alice', count: 42 }) | ||
|
||
expectTypeOf(result).toMatchTypeOf<{ | ||
container: HTMLElement | ||
component: { hello: string } | ||
debug: (el?: HTMLElement) => void | ||
rerender: (props: { name?: string; count?: number }) => Promise<void> | ||
unmount: () => void | ||
}>() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type * as Svelte from 'svelte' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type IS_MODERN_SVELTE = any extends Svelte.Component ? false : true | ||
|
||
/** A compiled, imported Svelte component. */ | ||
export type Component<P> = IS_MODERN_SVELTE extends true | ||
? Svelte.Component<P> | Svelte.SvelteComponent<P> | ||
: Svelte.SvelteComponent<P> | ||
|
||
/** | ||
* The type of an imported, compiled Svelte component. | ||
* | ||
* In Svelte 4, this was the Svelte component class' type. | ||
* In Svelte 5, this distinction no longer matters. | ||
*/ | ||
export type ComponentType<C> = C extends Svelte.SvelteComponent | ||
? Svelte.ComponentType<C> | ||
: C | ||
|
||
/** The props of a component. */ | ||
export type Props<C> = Svelte.ComponentProps<C> | ||
|
||
/** | ||
* The exported fields of a component. | ||
* | ||
* In Svelte 4, this is simply the instance of the component class. | ||
* In Svelte 5, this is the set of variables marked as `export`'d. | ||
*/ | ||
export type Exports<C> = C extends Svelte.SvelteComponent | ||
? C | ||
: C extends Svelte.Component<unknown, infer E> | ||
? E | ||
: never | ||
|
||
/** | ||
* Options that may be passed to `mount` when rendering the component. | ||
* | ||
* In Svelte 4, these are the options passed to the component constructor. | ||
*/ | ||
export type MountOptions<C> = IS_MODERN_SVELTE extends true | ||
? Parameters<typeof Svelte.mount<Props<C>, Exports<C>>>[1] | ||
: Svelte.ComponentConstructorOptions<Props<C>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": ["./tsconfig.json"], | ||
"exclude": [ | ||
"src/__tests__/render-runes.test-d.ts", | ||
"src/__tests__/fixtures/CompRunes.svelte", | ||
"src/__tests__/fixtures/TypedRunes.svelte" | ||
] | ||
} |