#How to make types available without imports?

1 messages · Page 1 of 1 (latest)

patent oak
#

Hi folks. I have a little project where I don't want to import types and instead would like to have them just available everywhere.

So I put them in src/types.ts and added to tsconfig.json:

  "include": ["src/types.ts"], 

which looks stupid, because TS should include everything from src, right?
Yet, as tsc surprised me many times before, I admitted that it could magically help, but... no, these types are not available to the rest of my project.

So, how to do it? Should I add magical ".d." letter in the name or what?

#

I feel like declare can do the trick, but, meh, declared things are not even type-checked, are they?

split cloud
#

TS should include everything from src, right?
no, you are free to name your fodler however you want, src is just the usual name, but it's not the default

#

you are probably thinking about baseUrl

#

So, how to do it? Should I add magical ".d." letter in the name or what?
no, .d.ts files are files that add types to JS code
but tbh, there is no real difference between .ts and .d.ts
you could very well put typings in .ts files, and code in .d.ts files and it would still work the same

#

So I put them in src/types.ts and added to tsconfig.json:
that's not what the includes property does
it simply adds the file to the list of files to use in the compilation
those files will be checked by the compiler, but that doesn't make them globally available

#

@patent oak

patent oak
#

Thanks, I'll have look @split cloud !

patent oak
#

Hooray! It finally worked.

#

Here is what one needs to do:

  1. In just any typescript file in the place that tsc observes put your decl inside declare global { ... }
  2. PROFIT
#

Ah, yeah, and one comment. If there are no exports in that file, just add export {};. But that's not necessary to mention as tsc would bark at you if you didn't.