Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit acbddfd

Browse files
authored
fix(types): adjust legacy types for eslint-plugin-svelte (#409)
1 parent 4b94019 commit acbddfd

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

‎.eslintrc.cjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ module.exports = {
2626
{
2727
files: ['*.ts'],
2828
parser: '@typescript-eslint/parser',
29+
parserOptions: {
30+
project: './tsconfig.json',
31+
},
2932
extends: [
30-
'plugin:@typescript-eslint/recommended',
31-
'plugin:@typescript-eslint/stylistic',
33+
'plugin:@typescript-eslint/strict-type-checked',
34+
'plugin:@typescript-eslint/stylistic-type-checked',
3235
'prettier',
3336
],
3437
},

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expectTypeOf } from 'expect-type'
2+
import { ComponentProps } from 'svelte'
23
import { describe, test } from 'vitest'
34

45
import * as subject from '../index.js'
@@ -36,4 +37,14 @@ describe('types', () => {
3637
unmount: () => void
3738
}>()
3839
})
40+
41+
test('render function may be wrapped', () => {
42+
const renderSubject = (props: ComponentProps<Component>) => {
43+
return subject.render(Component, props)
44+
}
45+
46+
renderSubject({ name: 'Alice', count: 42 })
47+
// @ts-expect-error: name should be a string
48+
renderSubject(Component, { name: 42 })
49+
})
3950
})

‎src/component-types.d.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
1+
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-redundant-type-constituents */
22
import type * as Svelte from 'svelte'
33

4-
type IS_MODERN_SVELTE = any extends Svelte.Component ? false : true
4+
type IS_MODERN_SVELTE = Svelte.Component extends (...args: any[]) => any
5+
? true
6+
: false
57

68
/** A compiled, imported Svelte component. */
79
export type Component<
@@ -14,21 +16,21 @@ export type Component<
1416
/**
1517
* The type of an imported, compiled Svelte component.
1618
*
17-
* In Svelte 4, this was the Svelte component class' type.
1819
* In Svelte 5, this distinction no longer matters.
20+
* In Svelte 4, this is the Svelte component class constructor.
1921
*/
20-
export type ComponentType<C> = C extends Svelte.SvelteComponent
21-
? Svelte.ComponentType<C>
22-
: C
22+
export type ComponentType<C> = IS_MODERN_SVELTE extends true
23+
? C
24+
: new(...args: any[])=>C
2325

2426
/** The props of a component. */
2527
export type Props<C extends Component<any, any>> = Svelte.ComponentProps<C>
2628

2729
/**
2830
* The exported fields of a component.
2931
*
30-
* In Svelte 4, this is simply the instance of the component class.
3132
* In Svelte 5, this is the set of variables marked as `export`'d.
33+
* In Svelte 4, this is simply the instance of the component class.
3234
*/
3335
export type Exports<C> = C extends Svelte.SvelteComponent
3436
? C

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /