Provide `declare` keyword support for class methods
See original GitHub issueSearch 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:
- Created 3 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@RyanCavanaugh No, this is a different idea. Let me give you another example:
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?@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: