Skip to content

Svelte compiler parse/walk type errors #10734

@nvlang

Description

@nvlang

Describe the bug

Looking at the Svelte compiler docs, I'd expect the following to work just fine:

import { parse, walk } from "svelte/compiler";

const ast = parse("content");

// `walk(ast, {` doesn't fare much better either
walk(ast.html, {
    enter: (node) => {
        if (node.type === "Text") {
            this.skip();
        }
    },
    leave: () => {},
});

However, tsc throws a bunch of type errors (see below).

Reproduction

https://github.com/nvlang/svelte-bug

Logs

index.ts:6:6 - error TS2345: Argument of type 'TemplateNode' is not assignable to parameter of type 'Node'.
  Type 'Text' is not assignable to type 'Node'.
    Type 'Text' is missing the following properties from type 'Property': key, value, kind, method, and 2 more.

6 walk(ast.html, {
       ~~~~~~~~

index.ts:8:13 - error TS2367: This comparison appears to be unintentional because the types '"CatchClause" | "ClassBody" | "Identifier" | "Literal" | "MethodDefinition" | "PrivateIdentifier" | "Program" | "Property" | "PropertyDefinition" | "SpreadElement" | "Super" | ... 59 more ... | "VariableDeclaration"' and '"Text"' have no overlap.

8         if (node.type === "Text") {
              ~~~~~~~~~~~~~~~~~~~~

index.ts:9:13 - error TS2532: Object is possibly 'undefined'.

9             this.skip();
              ~~~~


Found 3 errors in the same file, starting at: index.ts:6

System Info

System:
  OS: macOS 14.3.1
  CPU: ...
  Memory: ...
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 21.7.0 - /usr/local/bin/node
  Yarn: 1.22.21 - /usr/local/bin/yarn
  npm: 10.5.0 - /usr/local/bin/npm
  pnpm: 8.15.4 - ~/Library/pnpm/pnpm
Browsers:
  Brave Browser: 120.1.61.109
  Chrome: 122.0.6261.112
  Edge: 122.0.2365.80
  Safari: 17.3.1
npmPackages:
  svelte: ^4.2.7 => 4.2.12

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