Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type inferrence with override methods in child class #31470

Closed
dislido opened this issue May 20, 2019 · 1 comment
Closed

type inferrence with override methods in child class #31470

dislido opened this issue May 20, 2019 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@dislido
Copy link

dislido commented May 20, 2019

Use Cases

interface TestProps {
  foo(num: number): number;
}
class Test extends React.Component<TestProps> {
  /**
   * sometimes we override functions and keep their parameters unchanged
   * in this case, the type of `nextProps` should be `Readonly<TestProps>` but inferred as `any`
   * we have to specify its type `nextProps: TestProps`
   * is there any way to tell typescript that we need to keep its parameters unchanged?
   * and typescript can find its type from the overridden function
   */
  componentWillReceiveProps(nextProps) {
    nextProps.foo(123);
  }
}

image
image

Examples

in parent classes

interface TestProps {
  foo(num: number): number;
}
namespace React {
  class Component<T> {
    /**
     * Any function that override it will inferred as (T) => void by default 
     */
    [some keywords?] componentWillReceiveProps(nextProps: T);
  }
}

in child classes

interface TestProps {
  foo(num: number): number;
}
class Test extends React.Component<TestProps> {

  /**
   * use (Readonly<P>) => void declared at React.Component
   */
  [some keywords?] componentWillReceiveProps(nextProps) {
    nextProps.foo(123);
  }
}
@jcalz
Copy link
Contributor

jcalz commented May 20, 2019

Duplicate of #1373

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants