compiler-construction - 在 typescript 中是否可以让函数返回扩展它的类的类型

标签 compiler-construction typescript intellisense

export class Base {
    static getSomething():typeof this //<-- change this?
    {
        var type = typeof this;
        return new this['type']();
    }
}
export class Foo extends Base {
    publicVar:string = 'getThis!';
}

var foo:Foo = Foo.getSomething();
var success:string = foo.publicVar;

上面返回一个错误,因为编译器说 Foo.getSomething 将返回一个 Base,而不是 Foo。 我想知道是否有另一种方法可以让我调用 Foo 的静态函数,编译器知道该函数将返回 Foo。

当然,我可以在 Foo 中显式实现它,因此在扩展 Base 的每个类中,或者我可以简单地对其进行类型转换:

var foo:Foo = <Foo> Foo.getSomething();

但我想知道是否有一种方法无需执行这些操作,因为我将经常使用此方法

最佳答案

摆脱类型断言

如果遵循里氏替换原则,则不需要类型断言。

代码示例 1 - 可替换对象...

module Test {
    export class Base {
       publicVar: string = 'base';

       static getSomething() : Base  {
            //... read on...
       }
    }

    export class Foo extends Base {
        publicVar:string = 'getThis!';
    }
}

// No <Foo> type assertion needed
var foo: Test.Foo = Test.Foo.getSomething();

alert(foo.publicVar);

或者,您可以创建一个接口(interface),告诉您返回的对象将具有 publicVar 属性并返回该属性...

代码示例 2 - 界面

module Test {
    export interface IPublicVarable {
        publicVar: string;
    }

    export class Base {
       static getSomething() : IPublicVarable  {
            //... read on...
       }
    }

    export class Foo extends Base {
        publicVar:string = 'getThis!';
    }
}

// No <Foo> type assertion needed
var foo: Test.IPublicVarable = Test.Foo.getSomething();

alert(foo.publicVar);

获取实际类型

这并不能解决您遇到的另一个问题 - var type = typeof this; 不会为您提供运行时所期望的内容。它将为您提供 Function 而不是 Foo

要获取类型名称,您确实需要使用一个实例(如果您使用 Test.Foo ,则类型名称再次为 Function - 这对您来说不好),所以这是一个不完美的例子,使用两个不同的子类,它们都满足接口(interface),基于我的 Obtaining a Class Name at Runtime example :

module Test {
    export class Describer {
        static getName(inputClass) { 
            var funcNameRegex = /function (.{1,})\(/;
            var results = (funcNameRegex).exec((<any> inputClass).constructor.toString());
            return (results && results.length > 1) ? results[1] : "";
        }

        static getInstanceOf(inputClass) : Test.IPublicVarable {
            var name = Describer.getName(inputClass);
            return new Test[name]();
        }
    }

    export interface IPublicVarable {
        publicVar: string;
    }

    export class Base {

    }

    export class Foo extends Base {
        publicVar:string = 'foo class';
    }

    export class Bar extends Base {
        publicVar:string = 'bar class';
    }
}

var a: Test.Base = new Test.Foo();
var x: Test.IPublicVarable = Test.Describer.getInstanceOf(a);
alert(x.publicVar);

var b: Test.Base = new Test.Bar();
var y: Test.IPublicVarable = Test.Describer.getInstanceOf(b);
alert(y.publicVar);

关于compiler-construction - 在 typescript 中是否可以让函数返回扩展它的类的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20996686/

相关文章:

c# - 为什么添加局部变量会使 .NET 代码变慢

java - 为什么不能在方法中采用多个可变参数参数?

silverlight - 在其继承树中具有通用类的 UserControl

html - 如何去除 ionic 4 段指示器(底线)?请告诉我

c# - 相当于 Promise.then() 的任务是什么?

visual-studio-2008 - ReSharper 4.5 取消了 VS 2008 的所有智能感知!

c++ - Visual Studio Intellisense 无法识别括号初始化中的类成员

compiler-construction - 什么是应用程序二进制接口(interface) (ABI)?

delphi - 如何检测编译exe的Delphi编译器版本?

javascript - Angular PWA 更新太慢