Why does angular-cli generate ngOnInit with no typing for the function result?
Asked Answered
C

1

6

I have a component generated by angular-cli and the resulting code for the .ts file has:

ngOnInit() {
}

instead of

ngOnInit(): void {
}

My tslint is configured to always want typedefs present, but the cli generates code without typedefs - that is my issue.

Is there a way to configure the cli to always set the required typedefs or are my only options are to disable this tslint check or fix it manually?

Chilton answered 4/9, 2017 at 21:50 Comment(2)
This has nothing to do with Angular per se. It's an issue relating to angular-cli and/or tslint. By the way, the term "typedef" is not used in TypeScript.Ethanethane
Yeah you are correct - I used typedef because it's the name of the tslint rule for having types at certain places.Chilton
P
5

Angular CLI uses blueprints to generate artifacts, and there is a directory @angular/cli/blueprints in your node_modules. For example, there is a subfolder component/files/path where you can find a template for the component generation. The Angular CLI team promises to allow creating custom blueprints in the future, but as of today, you can't just modify the template.

If you just need to add void to the method signature, do it manually or disable the corresponding tslint rule.

The other hacky way would be to use the library angular-cli-tools for template generation ( see https://github.com/littleuniversestudios/angular-cli-tools), but since you just want to add the void return type just do it manually.

Pushy answered 5/9, 2017 at 2:27 Comment(2)
My issue is that I cannot configure how the cli should generate my files to match my tslint options. And I don't know why angular written in typescript, did not make proper blueprints, because with the typedef rule of tslint you can force the user to always have types at certain places, so if :void was present in the blueprint it would always be correct not matter if the typedef rule was on of off. But anyway, thank you for the reply!Chilton
Create a feature request about adding void here github.com/angular/angular-cliPushy

© 2022 - 2024 — McMap. All rights reserved.