Skip to content
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

fix: add ambient types to published files #980

Merged
merged 8 commits into from
Apr 13, 2021
Merged

fix: add ambient types to published files #980

merged 8 commits into from
Apr 13, 2021

Conversation

ignatiusmb
Copy link
Member

Publish ambient types added by #917. Sorry, I didn't get to catch this before it was merged

  • Newly added ambient types isn't detected because the file doesn't exist
  • New projects created with TypeScript enabled will also complain that the modules doesn't exist
  • Plain JS projects still has autocompletion because it still has a hardcoded $app/ in its jsconfig.json

@dummdidumm
Copy link
Member

Thanks for catching this! Could you adjust the jsconfig as well?

@ignatiusmb
Copy link
Member Author

ignatiusmb commented Apr 12, 2021

Done! I suppose one changeset is enough? It's a different package, I'll update it then

@benmccann
Copy link
Member

I'm still having a really hard time understanding ambient.d.ts vs types.d.ts. I don't quite understand why there are two files or how we decide what goes in each one

The explanation on the original PR https://github.com/sveltejs/kit/pull/917/files/51a1212c29801a473304e06a3194a5d8f206f9ba#r611350994 was that ambient.d.ts must contain the top-level imports, but I don't see any imports in that file, so I'm not understanding yet

I also think the name "ambient" is a confusing way to name the file because as far as I understand the types in types.d.ts are also ambient - though I may be wrong on this point, so feel free to correct me

@ignatiusmb
Copy link
Member Author

It's probably just how types are organized within a certain codebase, there's really no right or wrong though as long as it works. But, having ambient modules certainly helps make it cleaner.

The name "ambient" simply means it is without implementation. It's similar to how compiled TypeScript will generate a .js and .d.ts files with declare keywords everywhere in the declaration file, meaning "assume there will be a variable named foo with type string already defined elsewhere (in this case, compiled .js file). We can also declare these manually (when not using tsc) in one .d.ts where we specify each modules (e.g. $app/env or $app/stores) and have it available everywhere in the project, which is what #917 did.

ambient.d.ts must contain the top-level imports, but I don't see any imports in that file

I think it's the other way around, it should not have any top-level imports. Perhaps it could be considered as global types in some ways where we usually declare module "module-without-declaration-types" to silence TypeScript in our global.d.ts file.

@benmccann
Copy link
Member

The name "ambient" simply means it is without implementation

That's what I thought. I believe the types in types.d.ts are also without implementation and are thus ambient. That makes it confusing to me to have a file called ambient.d.ts that contains only some of our ambient types. Maybe we could call it modules.d.ts or something more reflective of what the separation between the files actually is?

What's the issue with putting all the types in a single types.d.ts?

@ignatiusmb
Copy link
Member Author

ignatiusmb commented Apr 13, 2021

The types in types.d.ts are implemented with other files import-ing from it.

What's the issue with putting all the types in a single types.d.ts?

I'm not 100% certain on this, but declare module "$app/*" { ... } will not work when there's a top-level import/export as well. Even if it works, having them in a different file will also be easier to maintain and makes the separation between the types clearer. (Edit: Just double-checked and confirmed, it does not works)

Combining them in one types.d.ts would probably clutter the file too much, it's just like how we separate our utility functions into a separate file out of the logic file when it gets too big.

That makes it confusing to me to have a file called ambient.d.ts that contains only some of our ambient types. Maybe we could call it modules.d.ts or something more reflective of what the separation between the files actually is?

Why do you think a file called ambient.d.ts that contains only some of our ambient types? Isn't the point of naming it ambient.d.ts is to show that it only contains ambient types?

I think naming it modules.d.ts would be more confusing because according to ts(1208), "Add an import, export, or an empty 'export {}' statement to make it a module". Also, to quote a bit from TS docs about referencing a module

The compiler will try to find a .ts, .tsx, and then a .d.ts with the appropriate path. If a specific file could not be found, then the compiler will look for an ambient module declaration.

Which means any file with an import/export a module file, including plain .ts. On the other hand, ambient modules are typically defined in .d.ts files.


Is there anything else besides the naming and multiple type files? We could certainly put everything in one types.d.ts including types.internal and just publish that. We could also separate it even more into a types/ folder with an index.d.ts, publish the whole folder and point to the index.d.ts in it as the entry point.

@benmccann
Copy link
Member

I'm fine having two files and having them where they live. It's just the name that bothers me. Ambient is meaningless in my mind because every .d.ts file I've seen contains ambient types. I think it'd be helpful if you know from the name what might be contained within the file. E.g. if I see a file called server.ts I know roughly what will be in it. But with a file called ambient.d.ts I have no idea what to expect. It might as well be called types2.d.ts or anything else.

The TypeScript docs call the construct "ambient modules": https://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules. Would ambient-modules.d.ts work for you?

@dummdidumm
Copy link
Member

I did ambient.types.d.ts because they are then grouped at the bottom. I'm okay with ambient-modules. I'm against dropping ambient from the name because that's just the nomenclature.

Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the rename

@benmccann
Copy link
Member

hmm. looks like there's a merge conflict now

@dummdidumm
Copy link
Member

dummdidumm commented Apr 13, 2021

I generally agree that the types need some more cleanup. I like the idea of adding a types folder with index.d.ts. This would also help resolving some of the circular imports between the d.ts files. Should be a separate PR though.

@benmccann benmccann merged commit 4c45784 into sveltejs:master Apr 13, 2021
@ignatiusmb ignatiusmb deleted the fix/published-types branch April 13, 2021 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants