1

I'm curious could I make this generic optional somehow?

export type RequestType<T> = {
  readonly type: string;
  readonly value: string | T;
};

Because when I try to set object' type to RequestType it returns an error, that generic type requires 1 type argument.

1 Answer 1

1

You can provide a default type for T. For example:

export type RequestType<T = boolean> = {
  readonly type: string;
  readonly value: string | T;
}

Playground Link

The default value can even be string if you like, and thus value will be string | string, also known as just string.

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

4 Comments

I could, but I don't want to. I want it to remain string | T or just string.
@Cholewka You can set the default type to never to get that behavior.
or just string Then default it to string or never
Thank you, that's what I've been looking for :)

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.