Kevin Powell - CommunityKP-C
Kevin Powell - Community12mo ago
52 replies
vince

Quick question about date and string types

I have a date property (e.g: createdAt) from the backend. I'm sending it to the frontend as JSON data. Since JSON only works with strings, what's a good way to handle this as my TypeScript definitions are
Date
s. Should I rewrite my types to take Date | string or should I make some type of parsing function?

I feel like if I make it a Date | string, it'll be a bit ambiguous. I have multiple functions in the frontend that handle the createdAt as a
Date
, no parsing method needed. But when I get the createdAt from the backend, I'll need to know it's from the backend and parse it as a date in the frontend. Does that make sense? I'm trying to word it but I guess what I'm trying to get at is it might not be 'standardized' when working with it in the frontend. Typing that out, I realize what I could potentially do is rework my frontend methods that use createdAt to get the data from the backend, now that I actually have it setup, that way it'll be in a standardized format
Was this page helpful?