Skip to content

Provide declare keyword support for class methods #38008

Open

Description

@JasonHK

Search Terms

declare, class, method

Suggestion

I know that declare keyword already support class properties. I've wonder if it is possiable to bring the support to class methods as well?

Use Cases

You override a method a, and there are some other methods that return the return value of a.

Examples

This is just a simple example describing my idea.

class BaseTag
{
    constructor(tag: string) {...}

    static fromString(tag: string): BaseTag
    {
        return new BaseTag(tag);
    }

    static fromNumber(tag: number): BaseTag
    {
        return this.fromString(tag.toString());
    }
}

class ExtendedTag extends BaseTag
{
    static fromString(tag: string): ExtendedTag
    {
        return new BaseTag(`Tag: ${ tag}`);
    }

    declare static fromNumber(tag: number): ExtendedTag;

    // // Without `declare` support
    // static fromNumber(tag: number): ExtendedTag
    // {
    //     return super.fromString(tag) as unknown as ExtendedTag;
    // }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Activity

  1. RyanCavanaugh commented on Apr 16, 2020

    @RyanCavanaugh
    Member

    What you're asking for is #5863

  2. JasonHK commented on Apr 17, 2020

    @JasonHK
    Author

    What you're asking for is #5863

    Ryan Cavanaugh (@RyanCavanaugh) No, this is a different idea. Let me give you another example:

    class BaseClass
    {
        declare private version: string;
    
        public toJSON(): BaseInterface
        {
            return {
                version: this.version,
            };
        }
    
        protected toObject(): ObjectMixin<BaseInterface>
        {
            return Object.assign(this.toJSON(), { hello: true });
        }
    }
    
    interface BaseInterface
    {
        version: string;
    }
    
    class ExtendedClass extends BaseClass
    {
        declare private count: number;
    
        public toJSON(): ExtendedInterface
        {
            return Object.assign(super.toJSON(), { count: this.count });
        }
    
        declare protected toObject(): ObjectMixin<ExtendedInterface>;
    
        // // Without `declare` support
        // protected toObject(): ObjectMixin<ExtendedInterface>
        // {
        //     return super.toObject() as unknown as ObjectMixin<ExtendedInterface>;
        // }
    }
    
    interface ExtendedInterface extends BaseInterface
    {
        count: number;
    }
    
    type ObjectMixin<T> = T & { hello: boolean; };

    TypeScript Playground

    Although it seems that the example can be replaced by a generic, what if there are multiple return types that had to replace?

    I know I can use an interface with the name of the class to override the method, but this only works for public members, not protected or private ones.

    More importantly, if declare keyword already supports class properties, why don't add support to class methods as well?

  3. added
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    SuggestionAn idea for TypeScript
    and removed
    DuplicateAn existing issue was already created
    on Apr 17, 2020
  4. JasonHK commented on May 25, 2020

    @JasonHK
    Author

    Ryan Cavanaugh (@RyanCavanaugh) Should I write a proposal for this?

  5. whzx5byb commented on Oct 10, 2020

    @whzx5byb

    declare protected toObject: () => ObjectMixin<ExtendedInterface>; should work?

  6. JasonHK commented on Feb 20, 2021

    @JasonHK
    Author

    declare protected toObject: () => ObjectMixin<ExtendedInterface>; should work?

    whzx5byb Yes, this will work. But I hope declare methods would be supported as well.

    Sorry for the late reply 😅

  7. JasonHK commented on Feb 21, 2021

    @JasonHK
    Author

    declare protected toObject: () => ObjectMixin<ExtendedInterface>; should work?

    whzx5byb Well... There's a problem with this approach though, you cannot use a method to override a property, which means this will not work:

    class SuperClass
    {
        declare protected toObject: () => ObjectMixin<ExtendedInterface>;
    }
    
    class SubClass extends SuperClass
    {
        protected toObject(): ObjectMixin<...>
        {
            ...
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions