打字稿:重载类似级联的构造函数

编程入门 行业动态 更新时间:2024-10-28 08:21:21
本文介绍了打字稿:重载类似级联的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我需要将这些 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 09:36:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   类似   级联

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!