You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are two issues related to type safety when building Deaconn via next build. While these errors can be safely ignored and the website will operate without any major issues because they only appear with stricter overrides to the compiler options, this solution isn't ideal because we shouldn't be using variables and objects of type any anywhere with TypeScript. I will admit this is a bigger concern with another project of mine also utilizing the same frameworks used for Deaconn (Best Mods). The temporary solution involves commenting out the following configuration from the .eslintrc.cjs file.
We simply need to specify a valid type for the form parameter. However, this is more complicated since this components acts as a wrapper for all of our forms meaning the form types are different each time (e.g. different initiated values).
Reparsing Objects & Variables Via JSON.parse() When Passing As Prop
When passing a prop from Next's getServerSideProps() async function, it requires being able to serialize the prop's values via JSON. For example, say we have the following code.
The Article type includes a Date object field (createdAt) resulting in the above causing the following error.
Error: Error serializing `.article.createdAt` returned from `getServerSideProps` in "...".
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.
The solution I've always used was converting the object to a JSON string and then parsing the string as a JSON object. Typically, you'd use JavaScript's JSON.stringify() and JSON.parse() functions. However, JSON.parse() returns any which results in the following Next error when having the configuration mentioned at the beginning of the issue uncommented.
With that said, using JSON.parse() results in the createdAt field being set to a string instead of a Date object which is invalid type safety. There are a couple of things I've thought of doing before realizing this. I was initially trying to create a JSON validator that would validate JSON.parse() against a passed type and return the object as that specific type, but that was difficult (read below). However, as just stated, JSON.parse() sets the Date object to a string which renders the validator useless since we have another problem.
I believe I'm going to need to create new types for each object we pass via props that contains a string field (e.g. createdAtStr) that is set to the createdAt field as a string from the Date object. The components receiving the prop already parses the date as a string (e.g. const articleDate = new Date(article?.createdAtStr ?? Date.now())), so this should be all we need to do.
Looking Into Validating JSON.parse() (No Good)
I've read Stack Overflow threads (1, 2), but there isn't an easy solution without writing a lot of validation code alongside JSON.parse(). This answer from Stack Overflow is the best I could find and even then it's complicated. There are other options I explored such as using ZOD validation. However, since there are a lot of times we use this method to pass props along with many different types, I wasn't able to find something that easily converts a typical TypeScript type to a ZOD schema without writing out a schema for each type which would be quite annoying in my opinion.
I'll need to research these issues further and once I have updates, I will reply to this issue.
The text was updated successfully, but these errors were encountered:
There are two issues related to type safety when building Deaconn via
next build
. While these errors can be safely ignored and the website will operate without any major issues because they only appear with stricter overrides to the compiler options, this solution isn't ideal because we shouldn't be using variables and objects of typeany
anywhere with TypeScript. I will admit this is a bigger concern with another project of mine also utilizing the same frameworks used for Deaconn (Best Mods). The temporary solution involves commenting out the following configuration from the.eslintrc.cjs
file.Passing Formik Form As
any
TypeThe first issue relates to
src/components/forms/main.tsx
.Errors are as follows.
We simply need to specify a valid type for the
form
parameter. However, this is more complicated since this components acts as a wrapper for all of our forms meaning the form types are different each time (e.g. different initiated values).Reparsing Objects & Variables Via
JSON.parse()
When Passing As PropWhen passing a prop from Next's
getServerSideProps()
async function, it requires being able to serialize the prop's values via JSON. For example, say we have the following code.The
Article
type includes aDate
object field (createdAt
) resulting in the above causing the following error.The solution I've always used was converting the object to a JSON string and then parsing the string as a JSON object. Typically, you'd use JavaScript's
JSON.stringify()
andJSON.parse()
functions. However,JSON.parse()
returnsany
which results in the following Next error when having the configuration mentioned at the beginning of the issue uncommented.With that said, using
JSON.parse()
results in thecreatedAt
field being set to astring
instead of aDate
object which is invalid type safety. There are a couple of things I've thought of doing before realizing this. I was initially trying to create a JSON validator that would validateJSON.parse()
against a passedtype
and return the object as that specific type, but that was difficult (read below). However, as just stated,JSON.parse()
sets theDate
object to a string which renders the validator useless since we have another problem.I believe I'm going to need to create new types for each object we pass via props that contains a
string
field (e.g.createdAtStr
) that is set to thecreatedAt
field as a string from theDate
object. The components receiving the prop already parses the date as a string (e.g.const articleDate = new Date(article?.createdAtStr ?? Date.now())
), so this should be all we need to do.Looking Into Validating
JSON.parse()
(No Good)I've read Stack Overflow threads (1, 2), but there isn't an easy solution without writing a lot of validation code alongside
JSON.parse()
. This answer from Stack Overflow is the best I could find and even then it's complicated. There are other options I explored such as using ZOD validation. However, since there are a lot of times we use this method to pass props along with many different types, I wasn't able to find something that easily converts a typical TypeScript type to a ZOD schema without writing out a schema for each type which would be quite annoying in my opinion.I'll need to research these issues further and once I have updates, I will reply to this issue.
The text was updated successfully, but these errors were encountered: