After packaging a Svelte component library I ran into an issue where I failed my type checking. For example:
<Button>Click here</Button>
npm run check
> svelte-test@0.0.1 check
> svelte-kit sync && svelte-check --tsconfig ./tsconfig.json
====================================
Loading svelte-check in workspace: svelte-test
Getting Svelte diagnostics...
svelte-test/src/routes/+page.svelte:5:7
Error: Type '() => any' is not assignable to type 'never'. (ts)
<Button>Click here</Button>
====================================
svelte-check found 1 error and 0 warnings in 1 file
The issue is with the Button.d.ts file that is created when packaging the svelte components. For example the above Button component produces the following d.ts file:
import { SvelteComponentTyped } from "svelte";
import type { HTMLButtonAttributes } from 'svelte/elements';
export type ButtonMode = 'primary' | 'light' | 'accent' | 'accent-alt1' | 'accent-alt2' | 'accent-alt3' | 'outline' | 'plain';
export type ButtonProps = HTMLButtonAttributes & {
mode?: ButtonMode;
loader?: boolean;
round?: boolean;
block?: boolean;
action?: boolean;
size?: 'small' | 'medium' | 'large';
onButtonClick?: (event: ComponentEvent<undefined, HTMLButtonElement>) => void;
children?: Snippet;
};
import type { Snippet } from 'svelte';
import { ComponentEvent } from './';
declare const __propDef: {
props: Record<string, never>; // <!!! ----------- this should be `ButtonProps` ------------- !!!>
events: {
[evt: string]: CustomEvent<any>;
};
slots: {};
};
type ButtonProps_ = typeof __propDef.props;
export type ButtonEvents = typeof __propDef.events;
export type ButtonSlots = typeof __propDef.slots;
export default class Button extends SvelteComponentTyped<ButtonProps_, ButtonEvents, ButtonSlots> {
}
export {};
The issue is props: Record<string, never>; should be props: ButtonProps;
When used in a Record, never effectively creates an object type that can't have any properties, because no value can be assigned to a never type. This is why I'm getting:
Error: Type '() => any' is not assignable to type 'never'. (ts)
Reproduction
I created a public repo you can install and set up locally:
Initialize a new SvelteKit app with TypeScript
mkdir svelte-test
cd svelte-test
npx sv create #(with the following settings)
• SvelteKit minimal (barebones scaffolding for your new app)
• Yes, using Typescript syntax
Describe the bug
After packaging a Svelte component library I ran into an issue where I failed my type checking. For example:
The issue is with the
Button.d.ts
file that is created when packaging the svelte components. For example the above Button component produces the followingd.ts
file:The issue is
props: Record<string, never>;
should beprops: ButtonProps;
When used in a
Record
,never
effectively creates an object type that can't have any properties, because no value can be assigned to anever
type. This is why I'm getting:Error: Type '() => any' is not assignable to type 'never'. (ts)
Reproduction
I created a public repo you can install and set up locally:
Initialize a new SvelteKit app with TypeScript
Install package
Open
src/routes/+page.svelte
and add the followingAfter running the type check you should see the above type error...
Logs
System Info
Severity
blocking all usage of SvelteKit
Additional Information
No response