#how to import files from d.ts automatically from it's folder with file with same name

17 messages · Page 1 of 1 (latest)

dusty rain
#

I am trying to shift types definition to d.ts files without importing them.

I read the docs and it says if I add this, it will work

"include": ["src", "**/*.d.ts"]

But as expected, it import all d.ts files, which I don't want. There are some type with same name having different types causing issues.
For example this is folder structure

|constants
|--index.d.ts
|--index.ts
|index.d.ts

What I was looking for was, in file constants/index.ts I want types only from constants/index.d.ts and not index.ts that is at root level. How can I achieve it.

unreal stone
#

I am trying to shift types definition to d.ts files
why?

tall mesa
#

It's best to keep the types and the code co-located

dusty rain
#

I usually create a type file where I store them and then import them

unreal stone
dusty rain
#

@unreal stone
Is it not good practice to write common types in a separate file, if that's a case won't a d.ts file better than .ts file

unreal stone
#

it's not good practice or bad practice, but no, you would use a .ts file for that in a .ts project

#

it's recommended to put your types where you're using them, but that's just a recommendation

#

.d.ts is for ambient types

dusty rain
#

When I was reading compile behaviour,
It said that the d.ts files are not compiled, but .ts files are
Won't using d.ts file reduce bundle size?

unreal stone
#

not significantly, no

#

if you have a separate file for types you'll just get a file with export {} (if you're using esm)

#

if it's not in a separate file, it'll just get removed, so no difference

#

if you're using an actual bundler it won't matter anyways

dusty rain
#

I see, as all types get removed, it's just a blank file remains.
So I will just create .ts files to separate type definitions from components.

But out of curiosity, is it possible to achieve what I asked in the original question.

unreal stone
#

i don't work with .d.ts much, i don't know, sorry.
you can wait for someone that does know though