ts1206 decorators are not valid here, Angular 2

TypescriptAngular

Typescript Problem Overview


I started to program Angular 2 and I stuck with an error:

> ts1206 decorators are not valid here

@Component({   //  ts1206 decorators are not valid here
  selector: 'my-app',
  moduleId: module.id,
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})

Update:

My tsconfig.json:

 {
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

what can I do with it?

Typescript Solutions


Solution 1 - Typescript

The Decorators must come directly before an exported class for example:

@Component({
    ...
})
export class someComponent{}

this goes the same for @Pipe @Directive @Injectable and @NgModule

Solution 2 - Typescript

This error came to me when I used angular routing and defined routes after @NgModule decorator.

We need to define routes or any other decorater before the @NgModule decorator.

const appRoutes: Routes = [    // define this before @NgModule 
 { path: '',
   redirectTo: '/home',
   pathMatch: 'full'
 },
 { path: 'home', component: HomeComponent },
];


@NgModule({            // This Decorator should be just before an exported class 
declarations: [
 AppComponent,
 HeaderComponent,
 HomeComponent
],
imports: [
 BrowserModule,
 RouterModule.forRoot(
   appRoutes,
   { enableTracing: true } // <-- debugging purposes only
 )
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElior SastielView Question on Stackoverflow
Solution 1 - TypescriptJarod MoserView Answer on Stackoverflow
Solution 2 - TypescriptShubham JainView Answer on Stackoverflow