gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:04 24 4
gpt4 key购买 nike

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();

但我想知道是否有一种方法可以不必做这些事情,因为我会很多

最佳答案

摆脱类型断言

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

代码示例 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 - 接口(interface)

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com