-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Environment variables should be included in generated types #725
Comments
We can't generate types for environment variables because they are not globally unique - they're scoped to the specific target they're attached to. |
That's fair, then again you can access a resource on the type level even if it's not scoped to the target you're currently in and it will throw at runtime. I guess the same can be true here, but I agree it only really works if you're using the same env vars in all your functions (which I currently am). |
this might not be what you really want but you could do |
The secrets approach works until two functions need the same env var but with different values (I have a sentry helper function which grabs the dsn from the env instead of being passed as an argument). I was using /// <reference path="./../../sst-env.d.ts" />
declare module "sst" {
export interface Resource {
SentryRelease: string;
SentryDsn: string;
AppName: string;
FunctionOrigin: string;
}
}
export type {}; |
Just had a play around and have come up with a solutio to achieve better scoping of env vars in a monorepo setup. My setup is an "exports": {
".": "./src/index.d.ts",
"./env/cloudflare": "./src/env/cloudflare.d.ts",
"./env/sentry": "./src/env/sentry.d.ts",
}, My /// <reference path="./../../sst-env.d.ts" />
declare module "sst" {
export interface Resource {}
}
export type {}; Which just gives me the original generated types, then in /// <reference path="./../../sst-env.d.ts" />
declare module "sst" {
export interface Resource {
SentryRelease: string;
SentryDsn: string;
}
}
export type {}; This way, in packages that have a target which will use these env vars, you only need to include the specific export in your tsconfig: {
"compilerOptions": {
"types": [
"@repo/infra",
"@repo/infra/env/sentry"
]
},
} This way, you can scope env vars only to where they will be defined. Keep in mind, this approach falls apart if you have multiple targets in the same package who do not share all env vars. I think I prefer this approach rather than requiring sst to generate them |
Given that environment variables are linked to
Resource
(v0.1.1
) - I can access them in the app - it also makes sense they should be included in the generated types.The above config produces the following
sst-env.d.ts
But I can still access
Resource.WorkerName
in my worker as well as it being listed as a variable in the cloudflare dashboard:I should add, I'm not sure how the types should be generated if I reference the same variable in multiple functions
The text was updated successfully, but these errors were encountered: