Skip to content

Parser fails when a decorator follows a type-only declaration (Unexpected character '@') #20

@Chakroun-Anas

Description

@Chakroun-Anas

Description

When a decorator appears immediately after a TypeScript-only declaration such as a type or interface, the parser throws a syntax error — even though this is perfectly valid TypeScript syntax.

This seems to happen because the parser doesn’t correctly reset its internal state after handling a type-only declaration, so the next @ token is misinterpreted.


Reproduction

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

type User = {
  id: number;
  name: string;
  email: string;
}

@Injectable({
  providedIn: 'root', // makes the service available app-wide
})
export class UserService {
  private apiUrl = 'https://jsonplaceholder.typicode.com/users';

  constructor(private http: HttpClient) {}

  // Fetch all users
  getUsers(): Observable<User[]> {
    return this.http.get<User[]>(this.apiUrl);
  }

  // Fetch a single user by ID
  getUserById(id: number): Observable<User> {
    return this.http.get<User>(`${this.apiUrl}/${id}`);
  }
}

Output

Failed to parse source code: Unexpected character '@' (11:0)

Expected

The code should parse successfully. This is valid TypeScript and compiles fine in the TypeScript compiler and Babel’s TypeScript parser.

Environment

Node: 20.x
"acorn": "^8.15.0",
"@sveltejs/acorn-typescript": "^1.0.6"

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