Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWC Fails to Transpile Abstract Method named accessor #9835

Open
Shebuka opened this issue Jan 5, 2025 · 0 comments
Open

SWC Fails to Transpile Abstract Method named accessor #9835

Shebuka opened this issue Jan 5, 2025 · 0 comments

Comments

@Shebuka
Copy link

Shebuka commented Jan 5, 2025

Describe the bug

When using SWC with a Next.js project (v15), an abstract method in a TypeScript class causes a compilation error when visiting the page in the browser. The error message is:

Error:   × Expected '{', got 'abstract'

This issue occurs even though the code is valid TypeScript and compiles without issues using tsc. Renaming the method to something else (e.g., accessor_foo) resolves the issue, even though the original name is valid TypeScript.

Steps to Reproduce

  1. Create a TypeScript abstract class with an abstract accessor method::
export abstract class AbstractClass<T, V = any> {
  abstract accessor(data: T): V;
}
  1. Import and use this class in a Next.js project that uses SWC for transpilation.
  2. Run the project with npm run dev and visit the relevant page.

Debugging Steps Taken

  1. Renaming the method to accessor_foo resolves the error.
  2. Replacing the abstract accessor method with a concrete implementation resolves the error.
  3. The code compiles successfully with tsc without errors.
  4. Removing generics does not resolve the issue.
  5. Other abstract methods do not trigger this issue.

Environment

  • Next.js Version: 15.1.3
  • Node.js Version: 22.10.5
  • TypeScript Version: 5.7.2

Minimal Reproducible Example

export abstract class AbstractTest<T, V = unknown> {
  abstract id: string;
  abstract header: string;
  abstract accessor(data: T): V;

  printId() {
    return this.id;
  }
}

class Test extends AbstractTest<string, string> {
  id = "test-id-foo";
  header = "bar";
  accessor(data: string): string {
    return "42";
  }
}

export default function TestPage() {
  const test = new Test();
  return <div>{test.printId()}</div>;
}

Workaround

Renaming accessor to another name like accessor_foo or replacing with a concrete implementation resolves the issue but both are not ideal.

Input code

No response

Config

The issue persists even after ensuring the tsconfig.json is correctly configured:

{
  "compilerOptions": {
    "target": "esnext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true
  }
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.10.4&code=H4sIAAAAAAAAA22QzWrDMBCE73qKxScb8gOlJ9sN5NhbDyb3jXadiAYpSOskEPzulSw3bUNv0s4w8%2B3y7ey8AO6DeNQC%2BoQhwHb%2Bdhyk7RawgzcY7Kd1V7uBu4Ifv6Ea4tPYQ%2FN7fGQk9v9KqDWH4HxJKFhDV9Wwa1R0nKNV3qmspgYAzzJ4C3I0YWUoZYxqVCoTJjLgm7ClJ9xcuZirM66huEAhUV4aWvbOFSkuQyZlj36aPLHliOr78ZereH0pHlCcz0jc43AS6AerxTg7YX7ggeeltLMRO3HEVsvXSS%2BrFDOntmQum3tyrB73GNt1mjax6AsLoiWirwEAAA%3D%3D&config=H4sIAAAAAAAAA1WPSw7CMAxE9z1F5DUL1AUL7sAhouBWQfkpdiWqqncnf2AXv5nJ2MckBLxIwV0c6ZmGICNhHHMitDuW70SA94Ckog4Ml64yZYnjhoWcVQCWcUXOIaT5Os8tAMZ7woQXaQgbs9rpZf%2BtVN6GiET%2FxmyVbjUjXxun1grWP7citlPyunWDG3xNvWx8DJoePVnuOD9fu6hdFgEAAA%3D%3D

SWC Info output

No response

Expected Behavior

The page should load without errors, as the code is valid TypeScript.

Actual Behavior

The page fails to load, and the following error is shown:

Error:   × Expected '{', got 'abstract'

Renaming abstract accessor(data: T): V; to abstract accessor_foo(data: T): V; resolves the issue.

Version

1.10.4

Additional context

No response

@Shebuka Shebuka added the C-bug label Jan 5, 2025
@kdy1 kdy1 changed the title SWC Fails to Transpile Abstract Method in TypeScript Abstract Class SWC Fails to Transpile Abstract Method named accessor Jan 6, 2025
@kdy1 kdy1 added this to the Planned (Low priority) milestone Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants