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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744916956a4600894.html
Bar
interface to achieve declaration merging?class Bar extends Foo{}; interface Bar { foo(): number }
– STerliakov Commented Mar 7 at 17:08