How can i define a method in a class that it return only part of the class

frankw

sorry for my poor english; I'll show you the code;

class Service {
  word: string = "hi"
  get actions(): { hello: Function, greet: Function } {
    return this
  }
}
class TestService extends Service {
  hello() {}
  greet() {}
}
enter code here
const service = new TestService()
service.hello() // good
console.log(service.word) // error

how can i define a type that can makes actions only return all the Function properties? this might be a little confused, please notice that actions define in parent class, but i want its type are Part of inherit class's property

sorry for my poor description, i just update my code

H.B.

Anything that should not be visible outside the class can be hidden using private/protected:

abstract class Service
{
    private word: string = "hi"

    // These would be required, otherwise `return this` is an error in `actions`.
    abstract hello(): void;
    abstract greet(): void;

    get actions(): { hello: Function, greet: Function }
    {
        return this
    }
}
class TestService extends Service
{
    hello() { }
    greet() { }
}

const service = new TestService()
service.hello() // good
console.log(service.word) // error

As long as you are using classes directly you always get the full interface of the class. If you want to restrict the type more dynamically you probably need to work with functions returning interfaces/advanced types.

e.g.

type FunctionKeys<T> = {
    [K in keyof T]: T[K] extends Function ? K : never
}[keyof T]

function createService()
{
    class Service
    {
        word: string = "hi"
    
        hello() { }
        greet() { }
    
        get actions(): { hello: Function, greet: Function }
        {
            return this
        }
    }

    // Restricts the type to only the functions of Service
    const service: Pick<Service, FunctionKeys<Service>> = new Service();

    return service;
}


const service = createService()
service.hello() // good
console.log(service.word) // error

In this example, if a function is added to the Service class, it will automatically become available on the constructed instance.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I return self in a class method?

How can i call extra method that i define in anonymous class?

How can I define a method inside a class component in React?

How can I return a dependent type from templated class method?

How can I return a generic class from a method?

How can i make a method available only from within the class

How can I define a class in Python?

How can I define dynamic class member?

Can't define method in class

How can I define a containing class for another class in python?

How can I retrieve the return value of a method of a class within the same class?

How to define a method only when class generic type satisfies a test?

Can I define job instance using method instead of class Quartz?

How can I return self and another variable in a python class method while method chaining?

How to define method of class outside of class?

How to dynamically define the Class that can be used as a parameter in a method?

How to define a part of class name as macro?

How can I verify a class method signature?

How can I dynamically add a method to a class?

How can I tell if a method modifies the class?

Pascal - how can I return an array of a class from a function in that class?

specialize only (a part of) one method of a template class

How to define a method so that return the instance of the current class not of the class where it was inherited in Python?

How can I open class only to test class?

How to define a base class method only once, that when subclassed, uses members of the subclass and not the base class?

How Can I Call a Class Method From a Different Class in Python?

How can I call a method of one class to another class in android

How can I use a Method from a class in a different class?

Using Mockito, how can I mock a return value of an instance method without having an instance of that class?