0

In our application, I would like it to be optional to have to define method arguments, if those are known from a parent:

interface Parent<T> {

    foo(arg1: T): void;

}

class MyImpl implements Parent<string> {
    foo(arg1) {

    }
}

Currently this throws an error:

Parameter 'arg1' implicitly has an 'any' type.

I'm curious if there's a way to avoid this error.

The real-world use-case is a lot more complex, and making the argument type optional avoids a bunch of quite complicated type juggling.

There's other places in Typescript where it can infer, specifically:

type Callback = (s:string) => void;

function foo(cb: Callback) {
  cb('hi');
}

foo(arg => console.log(arg));

I'm hoping I can make this somehow work with subclasses.

1 Answer 1

2

Unfortunately it is not possible. There has been a discussion about it in this issue - https://github.com/Microsoft/TypeScript/issues/1373

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for providing a reference. Hoping that one day this answer is no longer correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.