简体   繁体   English

使用不同类型(非原始)的一个参数重载的Typescript方法

[英]Typescript method overloading with one parameter of different type (non-primitive)

Ok, what I would like to do is simple. 好的,我想做的很简单。 I want to overload a method so that it can take one parameter, which is of non-primitive type. 我想重载一种方法,以便它可以采用一个非原始类型的参数。

I understand I can't do it like this because TypeScript losts all types when compiled to JavaScript. 我知道我不能这样做,因为TypeScript在编译为JavaScript时会丢失所有类型。 If one of the parameters was primitive (eg string), I could do it (typeof myString == "string"). 如果参数之一是原始的(例如字符串),我可以做到(typeof myString ==“ string”)。

public doSomething(typeA: TypeA);
public doSomething(typeB: TypeB);
public doSomething(type: TypeA | TypeB) {
    if (typeof type == "TypeA") {
        ... // obviously I can't do that, Javascript got no types
        ... // typeof type === "object"
    }
    else if (typeof type == "TypeB") { //the same }
    else { //undefined }
}

What I thought up is this: 我想到的是:

public doSomething(typeA?: TypeA, typeB?: TypeB) {
    if (typeA) { ... }
    else if (typeB) { ... }
    else { //undefined }
}

Then I have to call this way: 然后我必须这样称呼:

doSomething(typeA, undefined); //to call TypeA version
doSomething(undefined, typeB); //to call TypeB version

Is such a design acceptable or is it better to rather create two separate methods? 这样的设计是否可以接受?还是最好创建两个单独的方法?

EDIT: 编辑:

This notation is probably better. 这种表示法可能更好。 You are forced (by IDE) to add the undefined when you're calling the method. 调用该方法时,IDE强制您添加未定义的方法。

public doSomething(typeA: TypeA | undefined, typeB: TypeB | undefined) {
    if (typeA) { ... }
    else if (typeB) { ... }
    else { //undefined }
}

You can have functions that check whether a value is of a certain type, and typescript has the notion of user defined type guards : 您可以具有检查某个值是否属于某种类型的函数,而typescript具有用户定义的类型guards的概念:

interface TypeA {
    x: string;
}
function isTypeA(value: any): value is TypeA {
    return Object.keys(value).length === 1 && typeof value.x === "string";
}

interface TypeB {
    y: number;
}
function isTypeB(value: any): value is TypeB {
    return Object.keys(value).length === 1 && typeof value.y === "number";
}

function doSomething(typeA: TypeA);
function doSomething(typeB: TypeB);
function doSomething(type: TypeA | TypeB) {
    if (isTypeA(type)) {
        console.log(type.x);
    } else {
        console.log(type.y);
    }
}

( code in playground ) 操场上的代码

In your example, as the passed value can be only of TypeA and TypeB then there's no need for the extra if/else. 在您的示例中,由于传递的值只能是TypeATypeB因此不需要多余的if / else。
But you can also use the isTypeB . 但是您也可以使用isTypeB

This method is good if you need this check in a few places in your code, but if you need it for just that one function, then I think that I would go with two different functions, depending on the amount of duplicated code. 如果您需要在代码中的几个地方进行此检查,那么这种方法是不错的选择,但是如果您只需要一个函数,那么我认为我会使用两个不同的函数,具体取决于重复代码的数量。

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

相关问题 Chrome探查器上的“不支持的非原始比较” - “Unsupported non-primitive compare” on Chrome profiler YDKJS 不断发展的非原始平等 - YDKJS up & going non-primitive equality 如何在WebMethod中接收非原始数据? - How to receive Non-primitive data in WebMethod? LocalStorage是否以任何方式支持自定义(即非原始类型)对象存储和检索? - Does LocalStorage support Custom (i.e. non-primitive type) Object Storage and Retrieval in any way? JavaScript中的所有对象都符合规范,但在DOM中,一个非原始对象不是。哪一个? - All objects in JavaScript are truthy per the spec, but in the DOM one non-primitive object is not. Which? JavaScript 中原始数据类型和非原始数据类型的区别 - Difference between Primitive and non-primitive datatypes in JavaScript 我们应该关心 JS 非原始值的可变性吗? - Should we care about JS non-primitive values mutability? 基本类型字符串上的TypeScript重载方法 - TypeScript overloading method on base-type string 使用 reduce() 与 forEach() 的非原始数组元素的总和 - Sum of non-primitive array elements with reduce() vs forEach() 如果 `toString` 返回非原始类型,它会被“忽略”是什么意思? - What does it mean that `toString` gets “ignored” if it returns a non-primitive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM