Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript fluent api builder多个参数(覆盖)不可能_Typescript_Typescript Typings - Fatal编程技术网

Typescript fluent api builder多个参数(覆盖)不可能

Typescript fluent api builder多个参数(覆盖)不可能,typescript,typescript-typings,Typescript,Typescript Typings,考虑以下在第三方库中声明的类。我无法修改此文件 declare class Foo<T> { methodA(foo: string): this methodB(): this; methodD(): this; methodD(type: string): this; } 但这仍然允许我做以下工作: let typeWithoutB!: WithoutB<any>; typeWithoutB.methodA('hello').me

考虑以下在第三方库中声明的类。我无法修改此文件

declare class Foo<T> {
    methodA(foo: string): this 
    methodB(): this;
    methodD(): this;
    methodD(type: string): this;
}
但这仍然允许我做以下工作:

let typeWithoutB!: WithoutB<any>;

typeWithoutB.methodA('hello').methodB() // should not be possible

编辑


发现
type F=Parameters
返回
[type:string]
。有解决这两种参数类型的选项吗?

我认为这里没有一个简单的答案。一种可能是回到经典的面向对象编程(OOP)并引入一种新的方法。因此,我们将现有类的一个实例包装到一个新类中,并在适当的地方转发调用。类似下面的代码。请注意,这假设MethodA和MethodD返回“this”

class FooFacade<T>
{
    private _foo: Foo<T> = new Foo<T>();
    methodA(foo: string): this { this._foo.methodA(foo); return this; }
    
    methodD(): this;
    methodD(type: string): this;
    methodD(type?: string): this {
        if (type !== undefined) {
            this._foo.methodD(type);
        } else {
            this._foo.methodD();
        }
        return this;
    }
}
类facade
{
private_foo:foo=new foo();
methodA(foo:string):this{this.\u foo.methodA(foo);返回this;}
methodD():这个;
methodD(类型:string):这个;
methodD(类型?:字符串):此{
如果(类型!==未定义){
本方法(类型);
}否则{
这个;
}
归还这个;
}
}

这种方法的明显缺点是有点笨重,根据Foo的实际复杂程度,可能需要做很多工作。一个优点是它可以让您完全控制如何调用第三方软件。

我认为这里没有一个简单的答案。一种可能是回到经典的面向对象编程(OOP)并引入一种新的方法。因此,我们将现有类的一个实例包装到一个新类中,并在适当的地方转发调用。类似下面的代码。请注意,这假设MethodA和MethodD返回“this”

class FooFacade<T>
{
    private _foo: Foo<T> = new Foo<T>();
    methodA(foo: string): this { this._foo.methodA(foo); return this; }
    
    methodD(): this;
    methodD(type: string): this;
    methodD(type?: string): this {
        if (type !== undefined) {
            this._foo.methodD(type);
        } else {
            this._foo.methodD();
        }
        return this;
    }
}
类facade
{
private_foo:foo=new foo();
methodA(foo:string):this{this.\u foo.methodA(foo);返回this;}
methodD():这个;
methodD(类型:string):这个;
methodD(类型?:字符串):此{
如果(类型!==未定义){
本方法(类型);
}否则{
这个;
}
归还这个;
}
}

这种方法的明显缺点是有点笨重,根据Foo的实际复杂程度,可能需要做很多工作。一个优点是它可以让您完全控制如何调用第三方软件。

我认为如果存在不应该调用的函数,您可能会遇到更大的问题。100%同意。。。不幸的是,我们使用的第三方库无法与其他库轻松交换。您考虑过为methodB使用私有函数吗?typescript中的虚拟methodD的定义如下:methodD(type?:string):这看起来像是重载使这几乎不可能…顺便说一下,这里有一个。我认为如果存在不应该调用的函数,您可能会遇到更大的问题。100%同意。。。不幸的是,我们使用的第三方库无法与其他库轻松交换。您考虑过为methodB使用私有函数吗?typescript中的虚拟methodD的定义如下:methodD(type?:string):这看起来像是重载使这几乎不可能…顺便说一下,这里有一个问题。是的,我认为这是在支持重载之前的唯一解决方案。是的,我认为这是在支持重载之前的唯一解决方案。
foo.methodD() // expected one argument but got 0
foo.methodD('ok') // this works, but above should also work
class FooFacade<T>
{
    private _foo: Foo<T> = new Foo<T>();
    methodA(foo: string): this { this._foo.methodA(foo); return this; }
    
    methodD(): this;
    methodD(type: string): this;
    methodD(type?: string): this {
        if (type !== undefined) {
            this._foo.methodD(type);
        } else {
            this._foo.methodD();
        }
        return this;
    }
}