Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): adjust legacy types for eslint-plugin-svelte #409

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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