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.

Allow overload signatures when writing class property initializers

See original GitHub issue

Search Terms

  1. typescript class bound function parameter overload
  2. typescript class bound function overload
  3. typescript class bound method overload
  4. typescript class bound arrow overload

Suggestion

It would be nice if we could overload bound arrow function methods in a class without the need to specifically declare a constructor and bind methods in it.

Use Cases

Examples

// existing valid implementation
// class `A` has method `a` bound to its instance in the constructor
class A {
  constructor(){
    this.a = this.a.bind(this);
  }

  a(a: string): string;
  a(a: string, b: string): string;
  a(a: string, b?: string): string {
    if(b) return a + b;
    return a;
  }
}

// suggested alternative implementation
// class `B` declares a bound arrow function `a` and its overloads without the need for a ctor
class B {
  a = (a: string): string;
  a = (a: string, b: string): string;
  a = (a: string, b?: string): string => {
    if(b) return a + b;
    return a;
  }
}

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 4 years ago
  • Reactions:9
  • Comments:8

github_iconTop GitHub Comments

2reactions
fffloriancommented, Jul 29, 2021

@zacnomore’s approach didn’t work for me.

What I did to resolve this:

interface Overload {
  (a: string): string;
  (a: string, b: string): string;
}

class A {
  a: Overload = (a: string, b?: string): string => {
    if (b) {
      return a + b;
    }
    return a;
  }
}
1reaction
legraphistacommented, Aug 10, 2020

You already can bind class methods with the arrow syntax in JS:

class A {
    a = () => {
        console.log(this);
    }   
}

And you can overload normal class methods in TS:

class A {
  a(a: string): string;
  a(a: string, b: string): string;
  a(a: string, b?: string): string {
    if(b) return a + b;
    return a;
  }
}

But you can’t have both; you can’t have a bound method and overload it too (syntax error):

class A {
  a = (a: string): string;
  a = (a: string, b: string): string;
  a = (a: string, b?: string): string => {
    if(b) return a + b;
    return a;
  }
}

I was looking to achieve that ^

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript function overloading - Stack Overflow
Only the three overloads are recognized by TypeScript as possible signatures for a method call, not the actual implementation. The ...
Read more >
Documentation - Classes - TypeScript
A field declaration creates a public writeable property on a class: ... There are just a few differences between class constructor signatures and...
Read more >
Object-oriented code - pybind11 documentation
Write only properties can be defined by passing nullptr as the input for the read ... To enable dynamic attributes for C++ classes,...
Read more >
Init only setters - C# 9.0 draft specifications - Microsoft Learn
This proposal adds the concept of init only properties and indexers to C#. These properties and indexers can be set at the point...
Read more >
Python Class Constructors: Control Your Object Instantiation
Class constructors are a fundamental part of object-oriented programming in Python. They allow you to create and properly initialize objects ...
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