简体   繁体   English

如何指定类构造函数的返回类型(例如使用代理)?

[英]How to specify return type of class constructor (e.g. using proxy)?

I'm pretty new to the Typescript world. 我是Typescript世界的新手。 Trying around with it a little more I stumbled across a problem when using a Proxy as a returned value from a class constructor. 使用时,整个问题试图与它周围的多一点,我偶然发现Proxy作为从类构造函数返回值。

Imagine the following code: 想象以下代码:

class Container {
  constructor() {
    return new Proxy(this, containerProxyHandler);
  }
}

const container = new Container();

container.sessionStorage = () => {
  return new SessionStorage('SESSION_ID');
};

container.session = factory(() => {
  return new Session(container.sessionStorage);
});

container.random = protected(() => {
  return Math.random();
});

The Container type is intended to be used as a dependency injection container. Container类型旨在用作依赖项注入容器。 So assigning the properties inside the Container class is not possible due to the variable amount of services it will store. 因此,由于Container将存储的服务数量可变,因此无法在Container类内分配属性。

When validated it complains about missing properties sessionStorage , session and random inside the Container type when assigning arrow functions to them. 验证后,它会抱怨在为它们分配箭头功能时,在Container类型内缺少sessionStoragesessionrandom属性。

Property 'sessionStorage' does not exist on type 'Container'

I could of course assign a type to the container variable myself via as or use a factory function, but find this approaches cumbersome for other people to use for example in case of library. 我当然可以自己通过as或使用工厂函数将类型分配给容器变量,但是发现这种方法对其他人来说很麻烦,例如在使用库的情况下。

const container = new Container() as { [key: string]: any };

Is there an easier solution for this without the need of extra boilerplate code? 有没有更简单的解决方案,而无需额外的样板代码?

You can create an interface for it: 您可以为其创建一个接口:

interface SomeType { 
  [name: string]: any; 
}

暂无
暂无

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

相关问题 如何使用另一个属性返回特定的Geojson要素属性,例如按名称返回经度属性? - How to return specific Geojson feature property using another property, e.g. return longitude property by name? jQuery双对象构造函数-例如$($(this)) - jQuery double object constructor - e.g. $($(this)) 类变量的正确类型(例如 fArr = Uint32Array) - Correct type for class variable (e.g. fArr = Uint32Array) 如何检查2位小数? 例如2.2.10 - how to check 2 decimals? E.g. 2.2.10 我是否应该(静态地)对第三方代码进行类型检查(例如使用Flow)? - Should I (statically) type-check Third-party codes (e.g. using Flow)? Dojo-通过使用不起作用的类来标识div来实现removeClass,例如query('。lb')。removeClass('hide'); - Dojo - removeClass by identifying div using class not working e.g. query('.lb').removeClass('hide'); 如何确定 HERE 中的 map 类型(例如正常、卫星)? - How do I determine the map type (e.g. normal, satellite) in HERE? 节点正确返回错误(例如,验证错误) - Node return errors correctly (e.g. validation errors) 如何基于属性(例如类别)从同名组中选择跨度 - How to select a Span from a same-name group based on attribute (e.g. Class) Bootstrap Datepicker:如何将课程(例如“活动月份”)添加到我要突出显示的特定月份 - Bootstrap datepicker: how to add class (e.g. “month active”) to specific months i want to highlight
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM