You're typing process.env wrong

  Рет қаралды 55,972

Matt Pocock

Matt Pocock

Жыл бұрын

Typing your environment variables with Zod is SUPER ergonomic and, if I say so myself, rather beautiful.
TS Playground: tsplay.dev/WYe7EW
Become a TypeScript Wizard with my free beginners TypeScript Course:
www.totaltypescript.com/tutor...
Follow Matt on Twitter
/ mattpocockuk
Join the Discord:
mattpocock.com/discord

Пікірлер: 204
@noheadphones123
@noheadphones123 Жыл бұрын
Honestly these short videos are replacing dumb memes and stuff for my daily quick watch or youtube shorts feed. So I really appreciate your work in these tid bits, not all will be useful to me but the seed is planted for future edge cases.
@boian-inavov
@boian-inavov Жыл бұрын
These shorts are quick, condensed and really useful. I love them! 👌
@NedCollyer
@NedCollyer Жыл бұрын
I was using the NodeJS namespace with the z.infer trick, however I found it better to do `export env = envVariables.parse(process.env)`, Then in my envVariables schema, I use transform or coerce - so my env becomes typed (eg, numbers, booleans etc from a .env file)
@seannewell397
@seannewell397 Жыл бұрын
I like this - it gives control and one place where settings change and are managed, still in a global/static way.
@chungweileong
@chungweileong Жыл бұрын
Well, there's actually a limitation to this approach, if you try to intialize something globally (for instance, database connection) when the server starts-up, you will notice that the `env` will always/sometimes return undefined, as it's not ready.
@mudscuffer
@mudscuffer Жыл бұрын
@@chungweileong Sounds like you have some other issue. Import loop? Or async parsing perhaps?
@GuyGooL4
@GuyGooL4 Жыл бұрын
@@chungweileong I think mutating the env or lazy loading it is a problematic practice, you could store lazy loaded or deferred stuff in another globally available variable. The approach of exporting env globally works fantastically when you rely on env variables that should be present on the program's initial start, whether it be a node server or even a frontend framework
@Sammi84
@Sammi84 Жыл бұрын
@@chungweileong don't mutate your env from inside your application. Env is for passing data to your application.
@zheil9152
@zheil9152 Жыл бұрын
I would rather import the parsed env from a separate file than mess with the global interface that expects strings. It’s not really much effort considering the intellisense just auto imports from the correct place anyway.
@fallenpentagon1579
@fallenpentagon1579 Жыл бұрын
and you can't forget to actually validate the process env. For example if you're code has multiple entry points you need to make sure that everyone of them actually calls envVariables.parse
@davidadamson1588
@davidadamson1588 Жыл бұрын
Matts approach seems to be quite elegant and is impressive. But what @zheil says, is the way! The usage of zod to simplify validation is really convenient though 🎉
@Gigusx
@Gigusx Жыл бұрын
@@davidadamson1588 To be honest, to me this jumps out as a great opportunity for Typescript to make some improvements to interaction of env vars with the rest of your code.
@davidadamson1588
@davidadamson1588 Жыл бұрын
@@Gigusx I am not sure if I am a little old school… But I prefer to have such things as configuration in its own place, like inside a class. This helps to comply with the SOLID principle which again ensures the correct usage of eg a strategy pattern. Since when done properly, it will allow me to simply exchange the source of configuration by swapping out the configuration strategy. Strategies could be: loading configuration from a file or env vars or a combimation of both or what ever suits your needs. Hardcoding against process.env is not a thing you should do (in my opinion). Code against abstractions not concretions. People might argue that this is a „java thing“, but it has the same advantages when used with typescript, especially when working on large scale projects.
@havokgames8297
@havokgames8297 Жыл бұрын
@@davidadamson1588 It has the same downsides too. You added complexity that you carry for the life of the project which you may never take advantage of. Put them in a class sure, but strategy pattern? abstractions? If you love acronyms then don't forget KISS.
@carlosconejo3880
@carlosconejo3880 Жыл бұрын
Thanks Matt! keep em coming, learning a ton from these vids!
@xypnox
@xypnox Жыл бұрын
Really appreciate the shorter format, yet clear explanations
@farahrayis5928
@farahrayis5928 Жыл бұрын
Incredible! And yes we love the short videos. Thanks
@nsuid7499
@nsuid7499 Жыл бұрын
Keep putting out more of these video. you're making great content
@SachinSomia
@SachinSomia Жыл бұрын
I really like these short videos!
@mattpocockuk
@mattpocockuk Жыл бұрын
Good to hear!
@MikeBifulco
@MikeBifulco Жыл бұрын
THIS IS SO HELPFUL - Matt, I can't tell you how much I've learned from your channel in a short amount of time. Thank you - keep up the great work!
@VerhoevenSimon
@VerhoevenSimon Жыл бұрын
I love these interesting short videos.
@pulserudeus7968
@pulserudeus7968 Ай бұрын
awesome find! thanks
@alexmalinin2387
@alexmalinin2387 Жыл бұрын
Awesome stuff!
@scumbag_kiko
@scumbag_kiko Жыл бұрын
I absolutely love this
@jakubmalinowski1474
@jakubmalinowski1474 Жыл бұрын
oh cmon man, id love to use that on my engineer degree project if only this video was created 2 months ago. I was so struggling with process env on typescript. This was so helpful either way, thank you
@danieltran7637
@danieltran7637 Жыл бұрын
God damn thanks a lot dude! I searched now the past few hours for it, but I wasn't able to figure it out by my own. And thank god I found your video. Subscribed your channel, so that I can hopefully see more such hepful content. :thumbsup:
@saifullahrahman
@saifullahrahman Жыл бұрын
wow! I recently gave up trying to type these env variables. Thanks a lot!!
@unknownChungus
@unknownChungus Жыл бұрын
In our team we use JOI. Works perfectly fine. We create functions and group similar env in its return obj and import that function and use it at the place we need.
@joe_xyz
@joe_xyz Жыл бұрын
Zod is such a great and incredibly useful library, I feel like I'm not exaggerating when I say it's currently the best package on npm by far.
@parlor3115
@parlor3115 Жыл бұрын
One thing to note about zod though, is that it sucks with cross field validation
@joshuakb2
@joshuakb2 Жыл бұрын
​@@parlor3115 wouldn't the refine method serve that need?
@uziboozy4540
@uziboozy4540 Жыл бұрын
@deepkit/type is far superior to that garbage
@souvlaki42
@souvlaki42 Жыл бұрын
I personally use cleanEnv from a package called envalid for my env variables. Keep up with your impressive videos.
@leo11877
@leo11877 Жыл бұрын
This is gold!
@alpakarosa3678
@alpakarosa3678 Жыл бұрын
I was always trying to understand why my process.env.VARIABLE could be string or undefined, this helped me a lot
@DrWarpMan
@DrWarpMan Жыл бұрын
This is really interesting, I think I will try this, I have been doing the second version which is exporting Zod parsed process.env. But what you did here is made it type safe and globally available without the need to export. Which is cool, but the downside is as you said they are all strings by default, and I prefer, when working with booleans or numbers, to really have them as numbers.
@Erandros
@Erandros Жыл бұрын
(UPDATE: WHOOPS, this is incorrect) Many js libraries that provide a build process parse things like `process.env.MY_ENV_VAR`, and replace them with the actual env var found at build time. If you export the parsed object that won't happen while @mattpocockuk 's solution will work with that. Without such feature, I'd agree with your solution and the lack of ability to transform data.
@davidadamson1588
@davidadamson1588 Жыл бұрын
@@Erandros no. Matts solution will not work as well when using such build tools! This is due to the fact, that process.env cannot be validated by zod (since env vars are getting replaced individually). Typing process.env globally without having the validation will result in pure nonsense.
@Erandros
@Erandros Жыл бұрын
@@davidadamson1588 sorry
@davidadamson1588
@davidadamson1588 Жыл бұрын
@@Erandros no reason to be sorry. I hope my comment did not offend you 😨
@Erandros
@Erandros Жыл бұрын
@@davidadamson1588 not at all, I just felt ashamed that I gave inaccurate information, but that's on me, dont worry
@some_user_880
@some_user_880 Жыл бұрын
So nice trick!
@BBStub3
@BBStub3 8 ай бұрын
amazing piece of code
@torickjdavis
@torickjdavis Жыл бұрын
Our team has been using the resulting parsed Zod object, but extending it further with coercions. Definitely interesting to override the global ProcessEnv interface.
@blueghost512
@blueghost512 Жыл бұрын
I was using this approach with NodeJS (no TS), but I started using TS with this approach and really got into problems with other types like numbers (PORT for example). So now I'm using an interface that is applied to a getter function, in the getter I can covert the var into real number, and also I added a default value functionality. So now I have a centralized function working as a getter for the env vars, that can cast types and use default values if you var is not set.
@novailoveyou
@novailoveyou Жыл бұрын
It's quite nice. I really like it
@emkisn
@emkisn Жыл бұрын
I love these small videos, please keep sharing these superb ideas with us! I already did something really similar to that and the only problem I had is if you add to this env object the isDev property based on the "NODE_ENV === development" when you build to production your bundle might not strip out the code you have that is only related to development
@emkisn
@emkisn Жыл бұрын
So then to make this work I just used the process.env.NODE_ENV directly for these cases, and other places I access the env properties from this custom object.
@dealloc
@dealloc Жыл бұрын
@@emkisn Yeah I wouldn't use this for env variables I don't control. NODE_ENV being an example of this, or even PATH in POSIX because it shouldn't matter that they don't exist in those cases, there should be a reasonable fallback for those cases.
@emkisn
@emkisn Жыл бұрын
@@dealloc Makes sense, the only thing that borders me is that then we will have two different ways to access env variables, so it's not really consistent. Maybe an ESLint rule to only allow certain kinds of it usages like NODE_ENV could help with that.
@Mitsunee_
@Mitsunee_ Жыл бұрын
you could easily export transform env vars from such a file. For example for a positive integer you'd do: z.string().regex(/\d+/).transform(val => +val) the refine method of zod could also be used for more complex values instead of regex. Now all I need is some presets for frameworks such as astro and Next.js :)
@DEV_XO
@DEV_XO 11 ай бұрын
Amazing!
@burnhard2
@burnhard2 Жыл бұрын
This is great! What do you think of using `z.input` instead? Then you could even do transforms, for example to convert CUSTOM _STUFF to a number, while still retaining autocomplete and correct types on process.env. z.infer is an alias of z.output, but sometimes you want the input types instead, such as on process.env.
@antontimeboy6094
@antontimeboy6094 Жыл бұрын
You can also transform the env variables inside the zod schema, from strings to eg. Date. You won't be able to do the `interface extends ...` trick, because that still only accepts strings.. But you can parse the envs and export them, just like you did for a second there.
@thisweekinreact
@thisweekinreact Жыл бұрын
nice, didn't think about it 👌
@balduin_b4334
@balduin_b4334 Жыл бұрын
the t3 stack also does this, but with an own "env" variable that is being exported. Also the declare 2 schemas: client schema and server schema. i find it pretty useful (yes I know the client schema is a little bit more expense, variable has to start with "NEXT_PUBLIC_..." and so on )
@wfl-junior
@wfl-junior Жыл бұрын
I never thought of this approach and I really like it, this video timing was pretty good actually, I had an issue today in production because of an environment variable that I forgot to add in production, this would've helped me find the issue faster. I'm going to do this from now on, great stuff!
@gularz1982
@gularz1982 Жыл бұрын
So cool!
@Learnbynet
@Learnbynet Жыл бұрын
nice one
@gdnight
@gdnight Жыл бұрын
Have you heard of the znv package ? It does all of this and then some. Uses zod, parses into whatever type you're expecting your variable to be, and lets you provide defaults for each environment that NODE_ENV might be set to.
@jubayerabdullahjoy2582
@jubayerabdullahjoy2582 Жыл бұрын
KZfaq should look into adding a love react button, this video requires so much of it! Thanks Matt, this is awesome!
@wlockuz4467
@wlockuz4467 Жыл бұрын
Mind blown!
@skylerdjy
@skylerdjy Жыл бұрын
The parse would also fail and prevent the app from starting; instead of running into a runtime error or bug down the line, if the environment variables are not defined, which is also another benefit!
@somebody656
@somebody656 Жыл бұрын
I prefer exporting the parsed variable so that I can also do z.coerce.number() or something similar which avoid repeatedly doing that
@DjLeonSKennedy
@DjLeonSKennedy Жыл бұрын
Nice!!
@juliusmarminge
@juliusmarminge Жыл бұрын
This is great although it lacks the support of transforms, for example of boolean coercions, using default values etc. Due to the lack of transforms, a more correct way to infer the schema here is to use the _input type of the validator so that no transforms are affectign the types of the process.env. But this is a nevertheless a great way to get a simple validation that you're not missing any envs on build!
@AlvarLagerlof
@AlvarLagerlof Жыл бұрын
Cool comment
@MelroyvandenBerg
@MelroyvandenBerg 3 ай бұрын
I migrating to Fastfiy, and I'm also considering Zod now. It seems very superior to alternatives also for documenting API end points / schemas.
@MelroyvandenBerg
@MelroyvandenBerg 3 ай бұрын
ps. Hopefully this Zod + process env also works together with dot env eg. @fastofy/dot-ev?
@kalelsoffspring
@kalelsoffspring Жыл бұрын
I've seen a lot of developers who are fine duplicating static information like that, this is so much nicer :)
@MarcoBiedermann
@MarcoBiedermann Жыл бұрын
I've used `env-var` in the past to validate, parse and type my environment variables
@CanRau
@CanRau Жыл бұрын
You could do z.coerce.number() or a transform, or refine
@Luxcium
@Luxcium Жыл бұрын
I love TypeScript and I wish I could be there to understand like all much about it definitely it is wizardry 😅😅😅😅
@najlepszyinformatyk1661
@najlepszyinformatyk1661 Жыл бұрын
nice approach actually i did follow an export approach
@Ked_gaming
@Ked_gaming 11 ай бұрын
With zod you can infer types so you could do: CUSTOM_STUFF: z.infer.number() And it would do the equivalent of String(process.env.CUSTOM_STUFF) , so that's not really an issue. I still prefer exporting a dedicated object than messing with the node types but both options are acceptable to be honest
@KnThSelf2ThSelfBTrue
@KnThSelf2ThSelfBTrue Жыл бұрын
I don't think you can ever get rid of process.env, and this is the best way to handle it, but for env vars in general, I like to have a solution with one more trick up its sleeve: Wrapping the var in a getter that is refetched/decrypted from a config/secret store at the time of access (with reasonable amounts of in-memory caching where necessary) and then garbage collected. That way, if the node is compromised, the secrets are not on disk, and only a temporary access token can be stolen.
@codinginflow
@codinginflow Жыл бұрын
I use the envalid package for this
@codestalk9183
@codestalk9183 11 ай бұрын
this gives me ideas....
@RobertKonigsberg
@RobertKonigsberg 5 ай бұрын
I'm looking at creating environment variables that can be parsed by JSON. Seems there's a standard way to do that, and I wish the example you provided made it easy to do that, as well.
@jeffnikelson5824
@jeffnikelson5824 11 ай бұрын
sorry im new to this but what's the purpose with the zod libraries, can't I just write it with typescript?
@mattmarkus4868
@mattmarkus4868 Жыл бұрын
1:51 what is "types node"? Do you mean @node/types or something? I want to use this in our proj for urls and other global settings. Very cool. Thanks.
@abiodun6897
@abiodun6897 Жыл бұрын
so many things to learn lol
@GrSurnov
@GrSurnov Жыл бұрын
We can do something like this: import { z } from 'zod' const envSchema = z.object({ STR: z.string(), NUM: z.coerce.number(), }) declare global { namespace NodeJS { interface ProcessEnv extends Record {} } } const envVars = envSchema.parse(process.env) It results in for process.env.WHATEVER and full power of zod for the envVars
@brangtoggez6363
@brangtoggez6363 Жыл бұрын
what font did you use in the thumbnail ? Is that fira code ? it just looks so nice but fira code to me is not really that nice so i am curious though. Thanks
@ravenMK_
@ravenMK_ Жыл бұрын
Helpful video. Wondering how I could use that with a .env file? Would really appreciate support of this. Thanks
@mattpocockuk
@mattpocockuk Жыл бұрын
This is designed to work with a .env file!
@laluneodyssee4404
@laluneodyssee4404 Жыл бұрын
I like envalid for this solution
@Vendavalez
@Vendavalez Жыл бұрын
This seems helpful if you’re using envars for anything other than the main of your program, but that has turned out to be a bad idea every time I have done it so I just don’t do that anymore.
@roach_iam
@roach_iam Жыл бұрын
Is there any reason why this is better than "only adding it to the ProcessEnv"? Why use zod at all for this?
@nicolaichristensen6531
@nicolaichristensen6531 Жыл бұрын
If we forget an environment variable zod will just throw an error for us instantly and we won't have to debug and find out what's missing.
@thuytv3814
@thuytv3814 Жыл бұрын
I have that same question, what is the benefit of zod here...
@idonoD
@idonoD Жыл бұрын
What Nicolai said. Free Validation and typing is the key here
@dieguitto08
@dieguitto08 5 ай бұрын
Is there a way to do the same with ImportMeta?
@noamisaac
@noamisaac Жыл бұрын
The problem with this is that mutation don't work. If you export the parsed object and use it in place of environment variables, however...
@mattpocockuk
@mattpocockuk Жыл бұрын
Could you rephrase your question? It works great.
@artistry7919
@artistry7919 Жыл бұрын
Wym? You want to mutate the env variables?
@noamisaac
@noamisaac Жыл бұрын
@mattpocockuk look at libraries like envalid and envsafe. Instead of parsing the result and than doing nothing with the actual result object, we can parse the environment and than export it (I like to than add a path to it in the tsconfig, I.e. @/env) and use the resulting object instead of process.env. That way, any mutation you make to the environment variable (for example, using JSON.parse to turn a stringified object into a string proper) can be used
@PatrikTrefil
@PatrikTrefil Жыл бұрын
Great video. I would personally add the Readonly wrapper to make sure env vars are constant like this `interface ProcessEnv extends Readonly {}`.
@mattpocockuk
@mattpocockuk Жыл бұрын
Good idea!
@huynhthien4316
@huynhthien4316 Жыл бұрын
Do you have a way to make the key of process.env non-accessible if not defined? Ex: typescript will throw an error like if you define a constant object `as const` and you try to access the `key` that doesn't exist. I like this method, but unfortunately, `process.env` still allows access to the key that is not yet defined with zod, which makes accessing `process.env` directly riskier.
@wayneswildworld
@wayneswildworld Жыл бұрын
Wouldn't we want to use the implement keyword in this case?
@milon27
@milon27 11 ай бұрын
when i import .env file using doteng.config() in app.ts file i can access process.env.someting in most of the files. but few files for example helper.ts file or some file which are not directly calling from express router where i am not able to access process.env.something. env are not loading in these files why ? how to fix this issue
@mattpocockuk
@mattpocockuk 11 ай бұрын
Call dotenv.config() before you do your imports.
@chungweileong
@chungweileong Жыл бұрын
I actually like this approach, but the only problem I have with this is that it doesn't stop people from using a variable without define in the zod schema. Instead, I personally prefer to have a `env('....')` function, to use & validate variable on-demand, and allow you to do some cool stuff like throwing error when someone try to use non-NEXT_PUBLIC_ variable in NextJS frontend.
@dealloc
@dealloc Жыл бұрын
The problem with that is Next is not able to transform your `env(...)` calls during build time, which makes it unusable in client-side code. The same problem happens when during process.env[someArbitraryKey]. Next cannot inline those environment variables. Same if you were to destructure, or assign process.env to a variable.
@xtinctspecies
@xtinctspecies Жыл бұрын
Why not use a proper configuration management library like convict or dotenvsafe ?
@snatvb
@snatvb Жыл бұрын
nice :)
@mahadevovnl
@mahadevovnl Жыл бұрын
Can't you use Zod to enforce all env variables to be strings and never anything else?
@shahreazneeloy2119
@shahreazneeloy2119 Жыл бұрын
@tanishqmanuja
@tanishqmanuja Жыл бұрын
Saw this first in create T3 app, using it since then.
@mattpocockuk
@mattpocockuk Жыл бұрын
Do they do the declare global thing in T3? I thought they used the export const envVariables setup.
@tanishqmanuja
@tanishqmanuja Жыл бұрын
@@mattpocockuk Correct, it uses export const env setup with a variation that allows to throw error if env vars are accessed client side. But extending global namespace interfaces is certainly a neat idea.
@austincodes
@austincodes Жыл бұрын
This looks really familiar 🤔
@SergiySev
@SergiySev Жыл бұрын
What is `interface ProcessEnv` and how is it related to `process.env`?
@SergiySev
@SergiySev Жыл бұрын
@Clemens Horn thx, i was wandering from where `ProcessEnv` came, so apparently it is a part of NodeJS typings in typescript. good to know
@xtraszone
@xtraszone Жыл бұрын
I do it without using any other plugin using only typescript
@codefinity
@codefinity 11 ай бұрын
I employed this and it's great, but then I found `znv` module, so using that instead to keep it even simple🤓
@LewisCowles
@LewisCowles Жыл бұрын
Nope, use a module where you parse and export, then use that. no-process-env is an es-lint rule for a reason.
@ikarosouza
@ikarosouza Жыл бұрын
I find it better to import the parsed object
@crizo23
@crizo23 Жыл бұрын
Cool! I might be confused though - how does this give us "type safety"? Like you said, process.env.X is a string by default. It seems to me that the only benefit here is autocomplete. Am I wrong?
@mattpocockuk
@mattpocockuk Жыл бұрын
process.env.X is string | undefined by default!
@TechBuddy_
@TechBuddy_ Жыл бұрын
It gives you runtime safety by ensuring that the env variable you are accessing actually exists
@crizo23
@crizo23 Жыл бұрын
@@mattpocockuk ahh, true. Thanks!
@sankasanjeeva1182
@sankasanjeeva1182 11 ай бұрын
can i do without zod or yup?. just create an interface and pass.
@mathiasmaerker613
@mathiasmaerker613 Жыл бұрын
Actually you better parse the env at one place and export a decent schema because calling process.env involves calling C functions which is a negativ Performance impact when done too often
@sergeyzwezdin
@sergeyzwezdin 25 күн бұрын
Thank you for the video, but I think there is an issue. Imagine you have PORT: z.number() in your schema. While you extend ProcessEnv, typescript considers it as actual number, but in your process.env.PORT is still string, which can cause the bugs typescript unable to catch.
@mattpocockuk
@mattpocockuk 24 күн бұрын
Try it
@chhiethearith9525
@chhiethearith9525 Жыл бұрын
we can do this if you not using zod ==> declare global { namespace NodeJS { interface ProcessEnv { DB_HOST: string; DB_PORT: string; } } }
@mattpocockuk
@mattpocockuk Жыл бұрын
Yes, though this won't give you any runtime validation.
@r0n472
@r0n472 Жыл бұрын
Why do this instead of exporting and using the envVariables object to retrieve the environment variables?
@mattpocockuk
@mattpocockuk Жыл бұрын
I address this in the video itself. I find it just a bit more truthful. process.env.WHATEVER _is_ available, so why not make it type safe?
@scezar8880
@scezar8880 Жыл бұрын
@@mattpocockuk only issue is that if you add something like "DEBUG_LEVEL: z.number()" on the object, TS will assume that process.env.DEBUG_LEVEL is a number, which would be wrong
@mattpocockuk
@mattpocockuk Жыл бұрын
@@scezar8880 That's not correct. It'll conflict with the built-in ProcessEnv type, so it'll give you the proper error.
@karmasakshi
@karmasakshi Жыл бұрын
Nice. Or one can use envalid; works great and throws relevant errors.
@jakub7048
@jakub7048 11 ай бұрын
only legends use the npm package called 'envalid'
@anhvuuc8693
@anhvuuc8693 Жыл бұрын
NO, I will use the global type define :s
@RyanWaite28
@RyanWaite28 11 ай бұрын
I personally just create a singleton class with the values in the form i want: export class AppEnvironment { public static readonly DATABASE_URL: string = process.env['DATABASE_URL']; public static readonly CORS_WHITELIST: string[] = !process.env['CORS_WHITELIST'] ? [] : process.env['CORS_WHITELIST']; public static readonly PORT: number = process.env['PORT'] ? parseInt(process.env['PORT']) : 3000; public static readonly API_SECRET: string = (() => { if (!process.env['API_SECRET']) throw new Error(`API_SECRET not set in process.env`); return process.env['API_SECRET']; })(); public static readonly AWS = { S3_ARN: process.env['S3_ARN'], SES_ARN: process.env['SES_ARN'], }; }
@trapperxzw
@trapperxzw Жыл бұрын
Wouldn't it be better to just have a class model and use that as the source of truth throughout your app isolating it from using process.env ?
@kelvindecosta5350
@kelvindecosta5350 Жыл бұрын
Matt, you can also mention that since we're using `zod` here we can do things like this: DATABASE_URL: z.string().startsWith("postgres") I think these types would also be reflected in the NodeEnv interface!
@mattpocockuk
@mattpocockuk Жыл бұрын
Good tip! Though the types wouldn't be quite there
@marcoscarlomagno3065
@marcoscarlomagno3065 10 ай бұрын
I think it's cool for autocompletion but it's not a single source of truth, since you have to keep the .env file in sync with the types file, and actually that can cause issues in runtime (zod string trying to parse an undefined env variable). Correct me if I'm wrong
@mattpocockuk
@mattpocockuk 10 ай бұрын
The point of this is that zod ENSURES that your .env file is correct.
@Poxen
@Poxen Жыл бұрын
zod not needed for this? can just do the same thing with the declare global thing
@mattpocockuk
@mattpocockuk Жыл бұрын
Yep, true! Just wanted to show the awesome z.infer thing
@Erandros
@Erandros Жыл бұрын
not needed, assuming you're gonna typecheck process.env manually, otherwise you're assigning a type to it that isn't accurate
@Poxen
@Poxen Жыл бұрын
@@mattpocockuk oh yeah! that way u don’t need to add types manually, honestly could save a lot of confusion!
@JakobBjerrePetersen
@JakobBjerrePetersen Жыл бұрын
Why should I bother doing this in Zod instead of just creating a regular interface? (Honest question)
@mattpocockuk
@mattpocockuk Жыл бұрын
Because you'll be sure that at runtime, the correct env variables are being passed in
@JakobBjerrePetersen
@JakobBjerrePetersen Жыл бұрын
@@mattpocockuk Ah yes. That makes sense. Thanks for your answer!
@muhammadmejanulhaque3305
@muhammadmejanulhaque3305 Жыл бұрын
someone should just create a library or generator to do this automatically I think.
@elliotheath5366
@elliotheath5366 Жыл бұрын
Monorepo?
@mattpocockuk
@mattpocockuk Жыл бұрын
Yep, this'll just work
Infer is easier than you think
13:38
Matt Pocock
Рет қаралды 86 М.
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 195 М.
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 7 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 170 #shorts
00:27
БОЛЬШОЙ ПЕТУШОК #shorts
00:21
Паша Осадчий
Рет қаралды 4,8 МЛН
The purest coding style, where bugs are near impossible
10:25
Coderized
Рет қаралды 894 М.
We Fixed Environment Variables
7:27
Theo - t3․gg
Рет қаралды 61 М.
This Should Have Been In CSS Years Ago
1:57
Dev Dive In
Рет қаралды 33 М.
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 112 М.
Zod Goes Where TypeScript Can't
8:11
Theo - t3․gg
Рет қаралды 55 М.
TypeScript: Should you use Types or Interfaces?
4:06
Matt Pocock
Рет қаралды 149 М.
Learn tRPC in 5 minutes
6:04
Matt Pocock
Рет қаралды 103 М.
Can I Run Youtube Entirely From My Terminal? (No Browser)
15:31
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 383 М.
7 Awesome TypeScript Types You Should Know
8:57
Josh tried coding
Рет қаралды 77 М.
THEY WANTED TO TAKE ALL HIS GOODIES 🍫🥤🍟😂
00:17
OKUNJATA
Рет қаралды 7 МЛН