打字稿:重载类似级联的构造函数 [英] Typescript: Overloading cascade-like constructors

查看:39
本文介绍了打字稿:重载类似级联的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将这些 Java 构造函数重载转换为 Typescript:

I need to translate these Java constructor overloads to Typescript:

public QueryMixin() {
    this(null, new DefaultQueryMetadata(), true);
}

public QueryMixin(QueryMetadata metadata) {
    this(null, metadata, true);
}

public QueryMixin(QueryMetadata metadata, boolean expandAnyPaths) {
    this(null, metadata, expandAnyPaths);
}

public QueryMixin(T self) {
    this(self, new DefaultQueryMetadata(), true);
}

public QueryMixin(T self, QueryMetadata metadata) {
    this(self, metadata, true);
}

public QueryMixin(T self, QueryMetadata metadata, boolean expandAnyPaths) {
    this.self = self;
    this.metadata = metadata;
    this.expandAnyPaths = expandAnyPaths;
}

我已经尝试创建这些构造函数并查看那里,但我无法弄清楚如何获取它...

I've tried create these constructors taking a look over there, but I've not been able to figure out how to get it...

有什么想法吗?

constructor();
constructor(metadata: QueryMetadata);
constructor(metadata: QueryMetadata, expandAnyPaths: boolean);
constructor(self: T);
constructor(self: T, metadata: QueryMetadata);
constructor(???) {
    this.self = self;  <<< ???
    this.metadata = selfOrMetadata;  <<< ???
    this.expandAnyPaths = expandAnyPaths;
}

推荐答案

看来您真正想要的是支持现有或缺失参数的任意组合,并使用简单的默认值.通过 3 个参数,有 8 种开/关组合.因此,虽然 TypeScript 支持重载,但为了类型安全写出 8 个重载并不理想.

It seems what you actually want is to support any combination of present or missing parameters, with simple defaults. With 3 parameters, there are 8 on/off combinations. So while TypeScript supports overloading, writing out 8 overloads for type safety is not ideal.

但是使用命名参数(而不是位置参数)将简化实现.无需编写成倍增加的重载,即可轻松添加更多参数.

But using named parameters (instead of positional) will simplify the implementation. More parameters can be added easily without writing exponentially more overloads.

interface QueryMetadata { }
class DefaultQueryMetadata implements QueryMetadata { }

interface QueryMixinParams<T> {
    self: T;
    metadata: QueryMetadata;
    expandAnyPaths: boolean;
}

class QueryMixin<T> implements QueryMixinParams<T> {
    self: T;
    metadata: QueryMetadata;
    expandAnyPaths: boolean;

    constructor({
        self = null,
        metadata = new DefaultQueryMetadata(),
        expandAnyPaths = true,
        }: Partial<QueryMixinParams<T>> = {}) {
        this.self = self;
        this.metadata = metadata;
        this.expandAnyPaths = expandAnyPaths;
        console.log(this);
    }
}

// Any combination of parameters is supported
new QueryMixin();
new QueryMixin({});
new QueryMixin({ self: {} });
new QueryMixin({ self: {}, metadata: {} });
new QueryMixin({ self: {}, metadata: {}, expandAnyPaths: false });
new QueryMixin({ self: {}, expandAnyPaths: false });
new QueryMixin({ metadata: {} });
new QueryMixin({ metadata: {}, expandAnyPaths: false });
new QueryMixin({ expandAnyPaths: false });

在 TypeScript Playground 中尝试

这篇关于打字稿:重载类似级联的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆