-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Show examples of env.d.ts
as a module
#10945
Comments
Hi! Thanks for taking the time to report this issue.
Are you sure this is needed? It seems weird to me since Don't you think this is your other change that fixes your issue?
This is not a normal Typescript file, but an ambient module declaration. So, the problem with your approach is that your But, you're right, we should show how to use an import in your So the example will look more like: interface ImportMetaEnv {
readonly MY_VARIABLE: string;
}
interface ImportMeta {
readonly env: ImportMetaEnv;
}
namespace App {
interface Locals {
myLocalType: import("@bar/foo").LocalType<unknown>;
}
} This way the file remains an ambient module. |
I see, thanks for the explanation. The What is the difference between making |
I'm not gonna lie, I'm not an expert on how these things work. Last time I didn't immediately notice the difference... But I can try to explain it. An ambient module is used to inform a compiler that the declarations exists in a different place. This means you don't show the actual implementation. These files are not converted to JavaScript. Using a top level import turns that file into a normal module. Any code defined in a module is scoped to that module, and are not added to the global scope. That's why you need So yes, you can use a top-level import with I hope this helps a bit to better understand the difference... |
Yeah, I see the difference, it seems to be a difference only in intent. Thanks for the responses, can I close this Issue off? |
Hi, sorry for the delay. I think we can keep this open since it's still an issue. I'll link this issue to the open PR so we can close this issue once merged! |
📚 Subject area/topic
TypeScript support, middleware, environment variables
📋 Page(s) affected (or suggested, for new content)
https://docs.astro.build/en/guides/environment-variables/#intellisense-for-typescript
https://docs.astro.build/en/guides/middleware/#middleware-types
📋 Description of content that is out-of-date or incorrect
The documentation examples show the
env.d.ts
file as being a normal TypeScript file, not a module.This caused two problems for me:
1: To get the types in this file to actually apply, I had to add the "src" directory to the tsconfig "include" paths:
It looks like non-module scripts are not automatically included otherwise. Although I often see "src/**/*" being used, which also solves this problem, using "**/*" is the default in Astro.
2: You cannot import types from other modules using the
import { ... } from
syntax. This is not really a problem for environment variable definitions, because they don't include complex types. But it's a different story with context locals:Having the above definition and using
Astro.locals.myLocalType
fails to get the correct type (and thus gives me ESLint warnings) because it isn't imported.I suggest changing the examples so that this file becomes a module by default, just like the following example:
Thank you!
The text was updated successfully, but these errors were encountered: