1

I have an interface which requires a generic variable:

interface Action<T extends string> {
   type: T
}

I am able to extend it when I set the value of the generic:

interface SomeAction extends Action<"something"> {
}

I would like to infer the generic automatically, but I get an error when I try this:

interface SomeAction extends Action {
   type: "something"
}

Error TS2314: Generic type 'Action' requires 1 type argument(s).

I'd like the second example to infer its type automatically. Is this possible?

1 Answer 1

4

Just specify default value for T and it should work:

interface Action<T extends string = string> {}

Playground Link

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

3 Comments

@Eldar Are you sure? Can you provide some examples in which such a solution wouldn't work?
Sorry, my bad to make that assumption.
@Eldar It's okay, everyone can make a wrong assumption :)

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.