Let's consider this unique sample class:
class Unique {
public one(): Pick<this, "two" | "three"> {
return this;
}
public two(): Pick<this, "three"> {
return this;
}
public three(): string {
return "something else";
}
}
With the above setup, we can perform the following actions:
const unique = new Unique();
unique.one().two(); // Allowed
unique.two().one(); // Error: Property 'one' is not recognized on type 'Pick<Unique, "three">'.
The scenario outlined allows me to select which methods are chainable in the class, but I wonder if it's feasible to do so from the base of the class instance. In other words, is there a way for TypeScript to enforce that only the base can access the method one
, and restrict access to any other methods?
For example:
const unique = new Unique();
unique.one(); // Allowed
unique.two(); // Error: Property 'two' does not exist on type 'Unique'.
unique.three(); // Error: Property 'three' does not exist on type 'Unique'.
Any assistance on this matter would be greatly appreciated.