咨询ng generate class命令--type参数的可选取值及相关说明
--type Parameter for ng generate class Hey there! I’ve faced this exact confusion before—frustrating when the subcommand help doesn’t list a parameter you know exists, right? Let me clear this up for you:
Why you don’t see --type in ng g cl --help
The --type flag is a shared option across most Angular CLI generate commands (like component, service, class, etc.), not a class-specific one. That’s why it doesn’t show up in the help output for ng generate class specifically. If you run ng generate --help instead, you’ll find it listed under the shared options section.
What values can you use for --type?
Here’s the key thing: Angular CLI doesn’t enforce fixed allowed values for --type—it’s a flexible parameter that lets you add a suffix to your class filename and class name to clarify its purpose. You can use any string you want, but there are common community conventions that make your codebase more consistent:
model: For data model classes that represent core application data (e.g.,ng g cl user --type=modelcreatesuser.model.tswith classUserModel)dto: For Data Transfer Objects used to send/receive data between components or APIsentity: For classes that map directly to backend database entitiesutility: For general-purpose utility helper classesinterface: (Note: While this works, you’d usually useng g interfaceinstead for actual TypeScript interfaces—this is just if you need a class with an "interface" suffix for some reason)
Example usage
Let’s say you want a DTO for user login data:
ng g cl login-request --type=dto
This will generate login-request.dto.ts with the following class:
export class LoginRequestDto { }
The --type value simply gets camel-cased and appended to your class name, and kebab-cased (or preserved as-is, depending on your CLI version) in the filename.
内容的提问来源于stack exchange,提问作者Jo Paul

