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.

Experiment with always using parameters from base types for derived methods

See original GitHub issue

Note some related issues for doing this with properties rather than methods: #3667, #6118, #1373.

Goal

We want to make it easier for users to derive types without rewriting the same parameter signature every time

class Base {
  method(x: number) {
    // ...
  }
}

class Derived extends Base {
  method(x) {
    // 'x' should have the type 'number' here.
  }
}

Potential ideas

  • Only enable in noImplicitAny (doesn’t work for default initializers 😞)
  • Revert to any in all locations, opt in with another strictness flag (😞)
  • Something else? 😕

Potential issues

Default initializers with more capable derived types

class A {
  a = 1;
}

class B extends A {
  b = 2;
}

class Base {
  method(x: A) {
    // ...
  }
}

class Derived extends Base {
  method(x = new B) {
    x.b;
    // Today, 'x' has type 'B' which is technically unsound
    // but that's just what we do. Does changing this to 'A' break things?
  }
}

Default initializers that become less-capable via contextual types


class Base {
  method(x: "a" | "b") {
    // ...
  }
}

class Derived extends Base {
  method(x = "a") {
    // We have to make sure 'x' doesn't have type '"a"'
    // which is both unsound and less useful.
  }
}

Distinction between properties and methods

Would this work?

class Base {
  method = (x: number) => {
    // ...
  }
}

class Derived extends Base {
  method(x) {
    // Does 'x' have the type 'number' here?
  }
}

What about this?

class Base {
  method(x: number) {
    // ...
  }
}

class Derived extends Base {
  method = (x) => {
    // Does 'x' have the type 'number' here?
  }
}

Keywords: base type derived contextual contextually inherit methods implicit any

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:89
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

18reactions
nevircommented, Feb 6, 2019

I’m definitely curious about whether there’s been any more progress on this?

At least from my perspective, it seems like inference of method parameters from the interface would be a pretty big ergonomics win (and happens relatively frequently, especially in library/framework code)

13reactions
darklight9811commented, Jun 9, 2021

Any updates on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Controlled experiments (article) | Khan Academy
How are hypotheses tested? When possible, scientists test their hypotheses using controlled experiments. A controlled experiment is a scientific test done under ...
Read more >
Constraints on type parameters - C# Programming Guide
Why use constraints; Constraining multiple parameters; Unbounded type ... The type argument must be or derive from the specified base class.
Read more >
Base class pointer pointing to derived class object
The pointer of Base Class pointing different object of derived class: Approach: A derived class is a class which takes some properties from ......
Read more >
Inheritance and Composition: A Python OOP Guide
Derived classes must override the method to allow creating objects of their type. Implementation Inheritance vs Interface Inheritance. When you derive one class ......
Read more >
Overloading - C# in Depth
At compile time, the compiler works out which one it's going to call, based on the compile time types of the arguments and...
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