Skip to content

Commit

Permalink
fix(types): adjust legacy types for eslint-plugin-svelte (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous authored Nov 18, 2024
1 parent 4b94019 commit acbddfd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
7 changes: 5 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ module.exports = {
{
files: ['*.ts'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
},
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/stylistic',
'plugin:@typescript-eslint/strict-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
'prettier',
],
},
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/render.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expectTypeOf } from 'expect-type'
import { ComponentProps } from 'svelte'
import { describe, test } from 'vitest'

import * as subject from '../index.js'
Expand Down Expand Up @@ -36,4 +37,14 @@ describe('types', () => {
unmount: () => void
}>()
})

test('render function may be wrapped', () => {
const renderSubject = (props: ComponentProps<Component>) => {
return subject.render(Component, props)
}

renderSubject({ name: 'Alice', count: 42 })
// @ts-expect-error: name should be a string
renderSubject(Component, { name: 42 })
})
})
16 changes: 9 additions & 7 deletions src/component-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-redundant-type-constituents */
import type * as Svelte from 'svelte'

type IS_MODERN_SVELTE = any extends Svelte.Component ? false : true
type IS_MODERN_SVELTE = Svelte.Component extends (...args: any[]) => any
? true
: false

/** A compiled, imported Svelte component. */
export type Component<
Expand All @@ -14,21 +16,21 @@ export type Component<
/**
* 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.
* In Svelte 4, this is the Svelte component class constructor.
*/
export type ComponentType<C> = C extends Svelte.SvelteComponent
? Svelte.ComponentType<C>
: C
export type ComponentType<C> = IS_MODERN_SVELTE extends true
? C
: new (...args: any[]) => C

/** The props of a component. */
export type Props<C extends Component<any, any>> = 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.
* In Svelte 4, this is simply the instance of the component class.
*/
export type Exports<C> = C extends Svelte.SvelteComponent
? C
Expand Down

0 comments on commit acbddfd

Please sign in to comment.