Currently if I create a class that implements an interface, the class created will all methods not included in the interface. Here's an example:
interface ExampleTypes {
alpha();
}
class Example implements ExampleTypes {
alpha () {
return true
}
beta () {
return true
}
}
I am looking for a way to restrict the methods a given class can have.
This is something I also tried:
class ExampleSource {
alpha () {
return true
}
}
class Example implements Partial<ExampleSource> {
alpha () {
return true
}
beta () {
return true
}
}
And this:
class ExampleSource {
alpha () {
return true
}
}
class Example implements ExampleSource {
alpha () {
return true
}
beta () {
return true
}
}
Which is unintuitive. I'd like beta
to not be allowed in Example
.
This is the functionality that works but using a function and not a class:
interface ExampleType {
alpha?();
beta?();
}
This is value:
function Example(): ExampleType {
return {
alpha: () => true,
};
}
This throws a typescript error:
function Example(): ExampleType {
return {
alpha: () => true,
meow: () => true,
};
}
Ideally I can have this same functionality but with classes.
final
in TS – IlldefinedPartial<ExampleSource>
. But to actually try to restrict what methods a class contains is really suspect. – Illegality