Skip to content

How to type Middleware that add meta attributes? #4036

Answered by markerikson
renchap asked this question in Q&A
Discussion options

You must be logged in to vote

Yeah, both of those look plausible to me. I'd definitely encourage the use of type guards in general - both because you convince TS that the object is of a certain type inside the conditional, and also that the complex checks are encapsulated. Not sure how many places you're repeating these checks, but still worth it even if there's only one place.

The other option is to bypass TS's strictness and do something like `if (typeof (action as any)?.meta?.sound === "string"), etc. That would also definitely be worth encapsulating in a typeguard.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@renchap
Comment options

Answer selected by renchap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants