question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide `declare` keyword support for class methods

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
JasonHKcommented, Apr 17, 2020

What you’re asking for is #5863

@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?

0reactions
JasonHKcommented, Feb 21, 2021

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<...>
    {
        ...
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

The Method Declaration
A method's declaration provides a lot of information about the method to the compiler, the runtime system and to other classes and objects....
Read more >
Defining Methods - Learning the Java Language
The only required elements of a method declaration are the method's return type, name, a pair of parentheses, () , and a body...
Read more >
Defining Methods
A method declaration can provide more information about the method, including the return type of the method, the number and type of the...
Read more >
9. Classes — Python 3.11.1 documentation
Valid method names of an instance object depend on its class. By definition, all attributes of a class that are function objects define...
Read more >
Classes - JavaScript - MDN Web Docs
Class fields are similar to object properties, not variables, so we don't use keywords such as const to declare them. In JavaScript, private ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found