简体   繁体   English

扩展类时省略属性

[英]Omitting properties when extending a class

I am trying to derive a class to set a field as optional, with that field being required in the Base class.我正在尝试派生一个类以将字段设置为可选,该字段在基类中是必需的。

eg例如

class Base {
  param1! : string;
  param2! : string;
}

class Derived extends Omit<Base, "param1">{
} 

This results in error ts(2693):这会导致错误 ts(2693):

'Omit' only refers to a type, but is being used as a value here. 'Omit' 仅指一种类型,但在这里用作值。

In case this is an XY problem, what I am really trying to solve is:如果这是一个 XY 问题,我真正想要解决的是:

  1. I have generated classes that get regenerated on every build (so I cannot modify them manually, or do something to include the modification when generating)我已经生成了在每次构建时都会重新生成的类(所以我不能手动修改它们,或者在生成时做一些包含修改的事情)
  2. The generated classes define all properties as Required (similar to Base above).生成的类将所有属性定义为必需(类似于上面的Base )。
  3. I need to add decorators to some of the generated methods and properties.我需要为一些生成的方法和属性添加装饰器。
  4. I need to allow Partial instances of the Base class.我需要允许基类的Partial实例。 (Ideally with the decorators added only once). (理想情况下只添加一次装饰器)。

Ideally, I would extend the Base class, using Required , Omit , Pick , Partial in order to derive multiple classes omitting/requiring the correct properties.理想情况下,我会扩展 Base 类,使用RequiredOmitPickPartial以派生多个省略/需要正确属性的类。

I have almost given up on all this scaffolding/boilerplate stuff and want to manually write out the models/sql/REST by hand.我几乎放弃了所有这些脚手架/样板的东西,并想手动写出模型/sql/REST。

(I am new to typescript) (我是打字稿的新手)

TS has two scopes (worlds). TS 有两个范围(世界)。 First is for runtime values, second is for types.第一个是运行时值,第二个是类型。 According to JS specification and docs根据 JS 规范和文档

The extends keyword can be used to subclass custom classes as well as built-in objects. extends关键字可用于子类化自定义类以及内置对象。

It means, that after extends , both TypeScript and JavaScript expect runtime value.这意味着,在extends之后,TypeScript 和 JavaScript 都期望运行时值。 This is why you are getting an error, because Omit is a pure type, it is getting erased during compilation.这就是您收到错误的原因,因为Omit是纯类型,它在编译期间被删除。 Using Omit in this case is aquivalent to this pure javascript:在这种情况下使用Omit等同于这个纯 javascript:

class Derived extends {
}

This syntax is wrong.这种语法是错误的。 Instead, you need to use implements .相反,您需要使用implements THis keyword is from type scope and helps TS to validate class declaration. TH 这个关键字来自类型范围,帮助 TS 验证类声明。

class Base {
  param1!: string;
  param2!: string;
}

class Derived implements Omit<Base, "param1">{
  param2!: string;
} 

Regarding, decorators.关于,装饰师。 Please provide a minimum reproducible example with decorators.请提供带有装饰器的最小可重现示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用 class 装饰器在 TS 中扩展 class 时,我应该如何使用扩展的 class 属性? - When extending a class in TS using class decorators, how should I consume the extended class properties? 如何从 Typescript 中的 class 属性(省略方法)派生接口? - How to derive an interface from class properties (omitting methods) in Typescript? 在 TypeScript 中隐式扩展 JavaScript 类属性 - Extending JavaScript class properties in TypeScript implicitly 打字稿:扩展抽象类时的&#39;this&#39; - Typescript: 'this' when extending abstract class 打字稿:从父类获取扩展类的属性 - Typescript: Get properties of extending class from parent class 扩展抽象类时接口和类合并 - Interface and class merging when extending an abstract class 使用TypeScript装饰器扩展ES6类时扩展类型 - Extending type when extending an ES6 class with a TypeScript decorator 在使用非共享属性 (TypeScript) 时,从联合类型的对象中省略共享属性会导致错误 - Omitting a shared property from a union type of objects results in error when using non-shared properties (TypeScript) 在 Typescript 中扩展 class 时移除装饰器 - Remove decorator when extending class in Typescript 在typescript中扩展类时编译错误 - Compile error when extending a class in typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM