Skip to content

mount return value incorrectly typed? #14401

@Nearoo

Description

@Nearoo

Describe the bug

It appears that tsc doesn't accept the line:

const mounted: MyComponent = mount(MyComponent, { target: div })

with a type error. The following, however, works:

const mounted: MyComponent = mount(MyComponent, { target: div }) as MyComponent

Reproduction

The issue somehow cannot be reproduce in the svelte playground (attempt here), so I've created a repo showcasing the issue: https://github.com/Nearoo/svelte-mount-issue

If you prefer to recreate locally, do:

  • npm create vite@latest -> Svelte -> TypeScript
  • copy over App.svelte and MyComponent.svelte

Run npm run check, and you'll get the following output:

.../src/App.svelte:7:11
Error: Type '{ sayHello: (name: string) => void; } & { $set?: any; $on?: any; }' is not assignable to type 'SvelteComponent<{ sayHello?: ((name: string) => void) | undefined; }, { [evt: string]: CustomEvent<any>; }, {}> & { $$bindings?: string | undefined; } & { ...; }'.
  Type '{ sayHello: (name: string) => void; } & { $set?: any; $on?: any; }' is missing the following properties from type 'SvelteComponent<{ sayHello?: ((name: string) => void) | undefined; }, { [evt: string]: CustomEvent<any>; }, {}>': $$prop_def, $$events_def, $$slot_def, $destroy (ts)
  onMount(() => {
    const mounted: MyComponent = mount(MyComponent, { target: div })
  })


====================================
svelte-check found 1 error and 0 warnings in 1 file

Logs

No response

System Info

Just posting package.json:

{
  "name": "mount-ts-issue",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "check": "svelte-check --tsconfig ./tsconfig.json && tsc -p tsconfig.node.json"
  },
  "devDependencies": {
    "@sveltejs/vite-plugin-svelte": "^4.0.0",
    "@tsconfig/svelte": "^5.0.4",
    "svelte": "^5.1.3",
    "svelte-check": "^4.0.5",
    "tslib": "^2.8.0",
    "typescript": "^5.6.3",
    "vite": "^5.4.10"
  }
}

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions