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

Idiomatic organisation of types #347

Closed
bodymindarts opened this issue Jul 19, 2021 · 6 comments
Closed

Idiomatic organisation of types #347

bodymindarts opened this issue Jul 19, 2021 · 6 comments
Projects

Comments

@bodymindarts
Copy link
Member

Currently we have 1 file:

src/types.ts

from which we import the types.

An idiomatic way to do this would be:

@types/globalTypes/index.d.ts
@types/globalTypes/xxx.d.ts

Like this typescript should pick up the types automatically without needing to import them.

@vindard
Copy link
Contributor

vindard commented Jul 21, 2021

I can take a look at this next


Notes (:leaves: evergreen)

(adding below as I look into this)

On getting d.ts types into the global namespace

Linking the path:

Having imports inside d.ts file:


For additional consideration on global types as a common practice

I found some additional little comments when searching around on this. I'm not sure how viable or not they are, but I thought I'd include them here just in case we'd need to consider them for moving forward.

@vindard
Copy link
Contributor

vindard commented Jul 21, 2021

Like this typescript should pick up the types automatically without needing to import them.

@bodymindarts just to be clear on this, is this issue asking that we stop explicitly importing types and rather have all our own declarations available globally as well?

I started looking into this approach and it seems that one problem that would need to be managed is namespace clashes. For e.g. if the type exists globally already and someone re-declares it locally, I believe the existing global type simply gets extended, which can lead to some potential unexpected behaviours.

It also looks like this sort of functionality is usually reserved for vendor typings (if I understand correctly)


Edit 1: Adding feedback from 22/Jul Dev West call

Points made by Samer:

  • We do want to continue with global implicit types, and we prefer this over explicit imports
  • On the point of namespacing, we don't foresee this being a problem because ideally we would like to have more specific naming (generic names may be a code smell); this point ties into GraphQL API considerations as well

@vindard
Copy link
Contributor

vindard commented Jul 22, 2021

Implementation (:leaves: evergreen)

I've started playing with an implementation here.
.

Next steps:

  • resolve some issues I'm having when including import statements in the index.d.ts file
    import/export statements aren't supported in global declaration files, but import() function is (see Notes)

  • figure out which is better between using "paths" or "typeRoots" in the tsconfig.json file and figure out if node_modules and any other dirs need to be explicitly included in that setting
    we can simply use the "include" setting (see Notes)

  • move non-type values (likely into config.ts) and bring over the rest of the types/interfaces from types.ts

@bodymindarts
Copy link
Member Author

bodymindarts commented Jul 22, 2021

Some thoughts on this topic. I have to say I actually like explicit imports in general. It makes it easier to see the dependencies of file.
Am I correct in assuming that the dependency graph will still be clearly identifiable from the remaining imports of source code level dependencies even with global types?
I am a bit worried that global types will make it harder to detect things like circular dependencies.

That being said I think we certainly should adopt what is generally accepted as an idiomatic style from the ts community. @samerbuna has the most ts experience and is advocating for this approach. Could you perhaps post a link to 1 or 2 projects you consider doing this in an idiomatic way so we have something to refer to in this consideration?

I completely agree with the explicit type names argument regardless of scoping.
Will namespaces for types imported from 3rd parties mitigate the type shadowing issue? Would it be hard / break with idiomatic practice to also use namespaces in our own code?

@vindard
Copy link
Contributor

vindard commented Jul 25, 2021

I just added some additional notes to the 'Notes ( 🍃 evergreen)' comment above on:

  • importing/exporting in global declaration files
  • global type declarations as recommended practice

@bodymindarts bodymindarts added this to In progress in Dev Ongoing Jul 26, 2021
@vindard
Copy link
Contributor

vindard commented Jul 26, 2021

Based on chat with @samerbuna, we decided that the approach will be to:

  • have corresponding .d.ts files alongside regular files in modules as an ideal structure
  • temporarily have a global types.d.ts file for types that don't map cleanly to a single module

The goal will be to minimize, and if possible eventually remove the global types.d.ts file.

@bodymindarts bodymindarts moved this from In progress to Done in Dev Ongoing Aug 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

2 participants