admin管理员组文章数量:1405512
Imagine such code :
class Foo {
foo(): string | number { return 3 }
}
class Bar extends Foo {
foo(): number {
return super.foo() as number;
}
}
Would it be possible to just override the typing for the foo()
method in Bar
?
Something like
class Bar extends Foo {
override foo(): number;
}
So we don't have that unecessary super.foo()
in the implementation.
Imagine such code :
class Foo {
foo(): string | number { return 3 }
}
class Bar extends Foo {
foo(): number {
return super.foo() as number;
}
}
Would it be possible to just override the typing for the foo()
method in Bar
?
Something like
class Bar extends Foo {
override foo(): number;
}
So we don't have that unecessary super.foo()
in the implementation.
1 Answer
Reset to default 1There're several ways for example declare the method as an instance's prop or merge an interface. Not sure about consequences of the instance prop though, seems should be the same from the type POV.
Playground
class Foo {
foo(): string | number { return 3 }
}
class Bar extends Foo {
declare foo: () => number;
}
interface Baz{
foo(): number;
}
class Baz extends Foo {}
new Bar().foo
new Baz().foo
本文标签: typescriptTypeonly override on subclass methodStack Overflow
版权声明:本文标题:typescript - Type-only override on subclass method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744916956a2632054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Bar
interface to achieve declaration merging?class Bar extends Foo{}; interface Bar { foo(): number }
– STerliakov Commented Mar 7 at 17:08