-
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
- Loading branch information
Showing
10 changed files
with
133 additions
and
46 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
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,23 @@ | ||
import type * as Svelte from 'svelte' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type Component<P> = any extends Svelte.Component | ||
? Svelte.SvelteComponent<P> | ||
: Svelte.Component<P> | Svelte.SvelteComponent<P> | ||
|
||
export type ComponentType<C> = C extends Svelte.SvelteComponent | ||
? Svelte.ComponentType<C> | ||
: C | ||
|
||
export type Props<C> = Svelte.ComponentProps<C> | ||
|
||
export type Exports<C> = C extends Svelte.SvelteComponent | ||
? C | ||
: C extends Svelte.Component<unknown, infer E> | ||
? E | ||
: never | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export type MountOptions<C> = any extends Svelte.mount | ||
? Svelte.ComponentConstructorOptions<Props<C>> | ||
: Parameters<typeof Svelte.mount<Props<C>, Exports<C>>>[1] |
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" | ||
] | ||
} |