From acbddfdf3556dd6f411361f9ccfd85c9773c7fdc Mon Sep 17 00:00:00 2001 From: Michael Cousins Date: Mon, 18 Nov 2024 16:55:03 -0500 Subject: [PATCH] fix(types): adjust legacy types for eslint-plugin-svelte (#409) --- .eslintrc.cjs | 7 +++++-- src/__tests__/render.test-d.ts | 11 +++++++++++ src/component-types.d.ts | 16 +++++++++------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 91f6920..77f8b7c 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -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', ], }, diff --git a/src/__tests__/render.test-d.ts b/src/__tests__/render.test-d.ts index f8d1d90..5b1e16a 100644 --- a/src/__tests__/render.test-d.ts +++ b/src/__tests__/render.test-d.ts @@ -1,4 +1,5 @@ import { expectTypeOf } from 'expect-type' +import { ComponentProps } from 'svelte' import { describe, test } from 'vitest' import * as subject from '../index.js' @@ -36,4 +37,14 @@ describe('types', () => { unmount: () => void }>() }) + + test('render function may be wrapped', () => { + const renderSubject = (props: ComponentProps) => { + return subject.render(Component, props) + } + + renderSubject({ name: 'Alice', count: 42 }) + // @ts-expect-error: name should be a string + renderSubject(Component, { name: 42 }) + }) }) diff --git a/src/component-types.d.ts b/src/component-types.d.ts index 9df84c2..2e4a5a5 100644 --- a/src/component-types.d.ts +++ b/src/component-types.d.ts @@ -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< @@ -14,12 +16,12 @@ 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 extends Svelte.SvelteComponent - ? Svelte.ComponentType - : C +export type ComponentType = IS_MODERN_SVELTE extends true + ? C + : new (...args: any[]) => C /** The props of a component. */ export type Props> = Svelte.ComponentProps @@ -27,8 +29,8 @@ export type Props> = Svelte.ComponentProps /** * 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 extends Svelte.SvelteComponent ? C