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

feat(ofType): narrow type of output based on action type #749

Closed
wants to merge 1 commit into from

Conversation

thynson
Copy link

@thynson thynson commented May 13, 2021

See #622 (comment) for detail

@jayphelps
Copy link
Member

Thanks for the PR! Can you please add a detailed description on why this change is helpful and any downsides? The linked comment is helpful somewhat but not complete and also it's better to have everything here. Thanks again.

@thynson
Copy link
Author

thynson commented May 13, 2021

The reason behind the change is that a generic type param can only be inferred to literal type when it extends from a primitive type.

function foo<T extends string>(x: T): T {
  return x;
}

function bar<T>(x: T): T {
  return x;
} 

let x = foo('a'); // type of x is literal "a"
let y = bar('b'); // type of y is string

Copy link
Collaborator

@MaximeBernard MaximeBernard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you want to achieve and I think it's valuable for developer experience but I'm afraid the downside outweighs the upside.

I tried some Conditional Type

Type extends (Input['type'] extends string ? string : any),

but it doesn't work.

If you managed to make it work, we could go from here.

src/operators.ts Outdated
@@ -17,7 +17,7 @@ export function ofType<
// All possible actions your app can dispatch
Input extends Action,
// The types you want to filter for
Type extends Input['type'],
Type extends Input['type'] & string,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you can do that because

https://github.com/reduxjs/redux/blob/5f29b2667ff83f0189bc2aa304b214b4e19fa398/src/types/actions.ts#L18

export interface Action<T = any> {
  type: T
}

meaning it can be anything, not just a string (even though I agree the common usage is string)

If we do that, we won't be compatible with people not using string as their "type".

@thynson
Copy link
Author

thynson commented May 26, 2021

Hello, @MaximeBernard, thanks for your review.
I have updated it so that it should works for non-string action as well.

@MaximeBernard
Copy link
Collaborator

LGTM but I'm afraid I'm missing something. Any thoughts @evertbouw / @benlesh?

@evertbouw
Copy link
Member

evertbouw commented May 27, 2021

What happens if the type wasn't found? In other approaches it would silently narrow to unknown or never, something I'd like to avoid since it's not obviously wrong.

@thynson
Copy link
Author

thynson commented May 27, 2021

The template param Input extends from Action, so it should be rejected by the compiler. And on the situation that Input is inferred to non-literal type like Action<string>, the Output shall be the same with Input, which is the original behavior.

@evertbouw
Copy link
Member

image

If you use a number that wasn't part of the union then it does error in the way I was afraid of. The ofType call looks fine but then in the map function you suddenly have a never. This can be confusing.

For strings it seems to work just fine. The vast majority of Redux users will use strings so I can accept this.

@MaximeBernard
Copy link
Collaborator

Coming back after a few days, I just realized:

function foo<T extends string>(x: T): T {
  return x;
}

function bar<T>(x: T): T {
  return x;
} 

const x = foo('a'); // type of x is literal "a"
const y = bar('b'); // type of y is literal "b"

Your previous assessment of string is due to you using let instead of const.

I've went a step further and tried
image

which seems to infer litterals just fine.

Am I missing something?

@thynson
Copy link
Author

thynson commented Jun 6, 2021

Yeah, Typescript seems to also infer something to literal type in a const context.

But in a real-world scenario, one has to add as const to make Typescript infer it as literals with the original ofType.

of<MyAction>().pipe(
  ofType('a' as const),
  map((x) => x.foo), // okay, x is `{type: 'a', foo: string}`
);
of<MyAction>().pipe(
  ofType('a'), // not inferred to literals without `as const` 
  map((x) => x.foo), // does not compiled, x is still `MyAction`
);

@evertbouw
Copy link
Member

Closing old PRs. As always thank you for your contribution.

@evertbouw evertbouw closed this Dec 14, 2023
@thynson
Copy link
Author

thynson commented Dec 15, 2023

Any chance to get it reviewed and merged? @evertbouw
It just add extra function signature declaration to help the tsc to infer the type more accurately for DX improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants