Skip to content

Commit 90ba912

Browse files
authoredNov 19, 2024··
fix(types): do not erase component type constraint (#410)
1 parent acbddfd commit 90ba912

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed
 

‎.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
node-version: ${{ matrix.node }}
5353

5454
- name: 📥 Download deps
55-
run: ./scripts/install-dependencies ${{ matrix.svelte }}
55+
run: npm run install:${{ matrix.svelte }}
5656

5757
- name: ▶️ Run ${{ matrix.check }}
5858
run: npm run ${{ matrix.check }}

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public/bundle.*
44
coverage
55
dist
66
.idea
7+
*.tgz
78

89
# These cause more harm than good when working with contributors
910
yarn-error.log

‎CONTRIBUTING.md

+17
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ npm test
5858
npm run test:watch
5959
```
6060

61+
### Using different versions of Svelte
62+
63+
Use the provided script to set up your environment for different versions of Svelte:
64+
65+
```shell
66+
# install Svelte 5
67+
npm run install:5
68+
69+
# install Svelte 4
70+
npm run install:4
71+
72+
# install Svelte 3
73+
npm run install:3
74+
```
75+
76+
Not all checks will pass on `svelte<5`. Reference the CI workflows to see which checks are expected to pass on older versions.
77+
6178
### Docs
6279

6380
Use the `toc` script to ensure the README's table of contents is up to date:

‎package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@
7575
"build": "tsc -p tsconfig.build.json && cp src/component-types.d.ts types",
7676
"contributors:add": "all-contributors add",
7777
"contributors:generate": "all-contributors generate",
78-
"preview-release": "./scripts/preview-release"
78+
"preview-release": "./scripts/preview-release",
79+
"install:3": "./scripts/install-dependencies 3",
80+
"install:4": "./scripts/install-dependencies 4",
81+
"install:5": "./scripts/install-dependencies 5"
7982
},
8083
"peerDependencies": {
8184
"svelte": "^3 || ^4 || ^5 || ^5.0.0-next.0",

‎src/__tests__/render.test-d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ describe('types', () => {
1818
await rerender({ count: 0 })
1919
})
2020

21+
test('non-components are rejected', () => {
22+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
23+
class NotComponent {}
24+
25+
// @ts-expect-error: component should be a Svelte component
26+
subject.render(NotComponent)
27+
})
28+
2129
test('invalid prop types are rejected', () => {
2230
// @ts-expect-error: name should be a string
2331
subject.render(Component, { name: 42 })

‎src/component-types.d.ts

+28-16
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-redundant-type-constituents */
2-
import type * as Svelte from 'svelte'
2+
import type {
3+
Component as ModernComponent,
4+
ComponentConstructorOptions as LegacyConstructorOptions,
5+
ComponentProps,
6+
EventDispatcher,
7+
mount,
8+
SvelteComponent as LegacyComponent,
9+
SvelteComponentTyped as Svelte3LegacyComponent,
10+
} from 'svelte'
311

4-
type IS_MODERN_SVELTE = Svelte.Component extends (...args: any[]) => any
12+
type IS_MODERN_SVELTE = ModernComponent extends (...args: any[]) => any
513
? true
614
: false
715

16+
type IS_LEGACY_SVELTE_4 =
17+
EventDispatcher<any> extends (...args: any[]) => any ? true : false
18+
819
/** A compiled, imported Svelte component. */
920
export type Component<
10-
P extends Record<string, any>,
11-
E extends Record<string, any>,
21+
P extends Record<string, any> = any,
22+
E extends Record<string, any> = any,
1223
> = IS_MODERN_SVELTE extends true
13-
? Svelte.Component<P, E> | Svelte.SvelteComponent<P>
14-
: Svelte.SvelteComponent<P>
24+
? ModernComponent<P, E> | LegacyComponent<P>
25+
: IS_LEGACY_SVELTE_4 extends true
26+
? LegacyComponent<P>
27+
: Svelte3LegacyComponent<P>
1528

1629
/**
1730
* The type of an imported, compiled Svelte component.
1831
*
1932
* In Svelte 5, this distinction no longer matters.
2033
* In Svelte 4, this is the Svelte component class constructor.
2134
*/
22-
export type ComponentType<C> = IS_MODERN_SVELTE extends true
23-
? C
24-
: new (...args: any[]) => C
35+
export type ComponentType<C> = C extends LegacyComponent
36+
? new (...args: any[]) => C
37+
: C
2538

2639
/** The props of a component. */
27-
export type Props<C extends Component<any, any>> = Svelte.ComponentProps<C>
40+
export type Props<C extends Component> = ComponentProps<C>
2841

2942
/**
3043
* The exported fields of a component.
3144
*
3245
* In Svelte 5, this is the set of variables marked as `export`'d.
3346
* In Svelte 4, this is simply the instance of the component class.
3447
*/
35-
export type Exports<C> = C extends Svelte.SvelteComponent
48+
export type Exports<C> = C extends LegacyComponent
3649
? C
37-
: C extends Svelte.Component<any, infer E>
50+
: C extends ModernComponent<any, infer E>
3851
? E
3952
: never
4053

@@ -43,7 +56,6 @@ export type Exports<C> = C extends Svelte.SvelteComponent
4356
*
4457
* In Svelte 4, these are the options passed to the component constructor.
4558
*/
46-
export type MountOptions<C extends Component<any, any>> =
47-
IS_MODERN_SVELTE extends true
48-
? Parameters<typeof Svelte.mount<Props<C>, Exports<C>>>[1]
49-
: Svelte.ComponentConstructorOptions<Props<C>>
59+
export type MountOptions<C extends Component> = C extends LegacyComponent
60+
? LegacyConstructorOptions<Props<C>>
61+
: Parameters<typeof mount<Props<C>, Exports<C>>>[1]

0 commit comments

Comments
 (0)
Please sign in to comment.