You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询ng generate class命令--type参数的可选取值及相关说明

Understanding the --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=model creates user.model.ts with class UserModel)
  • dto: For Data Transfer Objects used to send/receive data between components or APIs
  • entity: For classes that map directly to backend database entities
  • utility: For general-purpose utility helper classes
  • interface: (Note: While this works, you’d usually use ng g interface instead 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:31:43