Skip to content

TS doesn't understand mount() return type #14966

@Bilge

Description

@Bilge

Describe the bug

If I assign the result of mount(Foo) to a variable typed Foo, TS gets confused and emits:

TS2322: Type '{ $on?(type: string, callback: (e: any) => void): () => void; $set?(props: Partial<Record<string, any>>): void; } & Record<string, any>' is not assignable to type 'Comp | undefined'.

Nevertheless, svelte-check finds no errors and my editor understands the return type of mount(Foo) is Foo. It is only TS compilation via webpack that is problematic.

Reproduction

new class {
    private foo?: Foo;

    constructor() {
        this.foo = mount(Foo, {target: document.body}); // TS2322
    }
}

Logs

Here are my tsconfig.json files.

{
  "extends": "..",
  "compilerOptions": {
    "verbatimModuleSyntax": true,
  },
}

(This is ..)

{
  "extends": "@tsconfig/recommended/tsconfig.json",
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2020",
    "moduleResolution": "node",
    "moduleSuffixes": ["", ".svelte"],
    "allowJs": true,
  }
}

Relevant portion of webpack.config.js.

module.exports = (env, options) => {
    return {
        module: {
            rules: [
                {
                    test: /\.tsx?$/,
                    loader: 'ts-loader',
                    options: {
                        compilerOptions: {
                            sourceMap: !prod,
                        },
                    },
                    resolve: {
                        extensions: ['.ts', '.svelte.ts', '.js'],
                    },
                },
                {
                    test: /\.svelte$/,
                    loader: 'svelte-loader',
                    options: {
                        emitCss: true,
                        preprocess: sveltePreprocess({
                            sourceMap: !prod,
                        }),
                        compilerOptions: {
                            dev: !prod,
                        },
                    },
                    resolve: {
                        extensions: ['.ts'],
                    },
                },
            ],
        },
    };
}; 

System Info

System:
    OS: Linux 5.15 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
    Memory: 8.36 GB / 11.69 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 22.12.0 - /run/user/1000/fnm_multishells/1717_1736443941005/bin/node
    Yarn: 1.22.22 - /run/user/1000/fnm_multishells/1717_1736443941005/bin/yarn
    npm: 10.9.0 - /run/user/1000/fnm_multishells/1717_1736443941005/bin/npm
  npmPackages:
    svelte: ^5.0.0 => 5.16.1
    webpack: ^5.11.0 => 5.97.1

Severity

blocking all usage of svelte

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions