From df520c61970b0bc3d9de81100fe4e6bd784a247b Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 3 Oct 2023 17:04:41 -0600 Subject: [PATCH 1/2] docs: clean up explanation --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f6f2cd3..42c46da 100644 --- a/README.md +++ b/README.md @@ -42,33 +42,35 @@ import { deepCamelKeys } from 'string-ts' import { camelCase, mapKeys } from 'lodash-es' import z from 'zod' -export const EnvSchema = z.object({ +const EnvSchema = z.object({ NODE_ENV: z.string(), }) function getEnvLoose() { - const rawEnv: { NODE_ENV: string } = EnvSchema.parse(process.env) + const rawEnv = EnvSchema.parse(process.env) const env = mapKeys(rawEnv, (_v, k) => camelCase(k)) // ^? Dictionary // `Dictionary` is too loose - // TypeScript is okay with this, 'abc' will be of type `string` + // TypeScript is okay with this, 'abc' is expected to be of type `string` + // This will have unexpected behavior at runtime console.log(env.abc) } function getEnvPrecise() { - const rawEnv: { NODE_ENV: string } = EnvSchema.parse(process.env) + const rawEnv = EnvSchema.parse(process.env) const env = deepCamelKeys(rawEnv) // ^? { nodeEnv: string } // Error: Property 'abc' does not exist on type '{ nodeEnv: string; }' // Our type is more specific, so TypeScript catches this error. + // This mistake will be caught at build time console.log(env.abc) } function main() { - getEnvLoose() // This will break at runtime - getEnvPrecise() // This will break at development time + getEnvLoose() + getEnvPrecise() } main() From 958aa2f80c0e57c579f73d08ccd33c99bd799ae0 Mon Sep 17 00:00:00 2001 From: Landon Yarrington Date: Tue, 3 Oct 2023 17:09:13 -0600 Subject: [PATCH 2/2] reword --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 42c46da..90c679e 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ function getEnvPrecise() { // Error: Property 'abc' does not exist on type '{ nodeEnv: string; }' // Our type is more specific, so TypeScript catches this error. - // This mistake will be caught at build time + // This mistake will be caught at compile time console.log(env.abc) }