Skip to content

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Sep 26, 2022

This fixes an issue where the data property of JsonRpcError wasn't properly inferred as optional, but rather as unknown. This is due to an issue(?) in TypeScript, where unknown | undefined is inferred simply as unknown, so Superstruct can't properly make the (type of the) field optional.

It's fixed by adding a new helper type, OptionalField, which explicitly makes a property (or multiple properties) of a type optional.

type JsonRpcError = Infer<typeof JsonRpcErrorStruct>; // { ..., data: unknown }
type JsonRcpError = OptionalField<Infer<typeof JsonRpcErrorStruct>, 'data'>; // { ..., data?: unknown }

This does not affect actual runtime validation of the types.

@Mrtenz Mrtenz requested a review from a team as a code owner September 26, 2022 16:12
@Mrtenz Mrtenz force-pushed the mrtenz/fix-error-type branch from 5fdef4f to 3b11f9e Compare September 28, 2022 11:27
@Mrtenz Mrtenz merged commit 94cc869 into main Sep 28, 2022
@Mrtenz Mrtenz deleted the mrtenz/fix-error-type branch September 28, 2022 13:39
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.

2 participants