When setting the sameSite
property of a cookie, it must be either Strict
, Lax
, or None
. However, the package I'm using uses lowercase values for this attribute. Therefore, I need to adjust the first letter of the string:
let sentenceCaseSameSite: "None" | "Lax" | "Strict" | undefined
if (
(sessionCookie.attributes.sameSite &&
sessionCookie.attributes.sameSite === "strict") ||
sessionCookie.attributes.sameSite === "lax" ||
sessionCookie.attributes.sameSite === "none"
) {
const firstLetter =
sessionCookie.attributes.sameSite[0].toUpperCase()
const sentenceCaseSameSite = (firstLetter +
sessionCookie.attributes.sameSite.slice(1)) as
| "None"
| "Lax"
| "Strict"
}
This approach involves using
as | "None" | "Lax" | "Strict"
, but it doesn't feel like the optimal solution.
Is there a method to capitalize the initial letter of the string while maintaining type safety?