#How to use types defined outside of ./node_modules/@types ?

70 messages ยท Page 1 of 1 (latest)

craggy loom
#

I Hey, i am not an expert when it comes to typescript and i am quite stuck for some hours now ๐Ÿ˜ฆ I am trying to use the types defined in a NPM package youtube-dl-exec which is under node_modules/youtube-dl-exec/src/index.d.ts but i simply do not know how to do so. The following is my configuration file:

{
  "extends": "./node_modules/gts/tsconfig-google.json",
  "compilerOptions": {
    "module": "ES2020",
    "moduleResolution": "node",
    "rootDir": "./src",
    "outDir": "build",
    "lib": ["es2019", "es6", "dom"],
    "types": ["reflect-metadata"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "include": [
    "src/**/*.ts",
    "test/**/*.ts"
  ],
  "ts-node": {
    "esm": true,
    "files": true
  }
}

I have already tried to:

  • Add the relative path to the index.d.ts file of the package into includes
  • Add the package to types
  • Add the package to typeRoots

No matter what i do, my VS Code cant resolve the type and my code does not work. Is this even possible to just simply import that types outside of @types?
I would be very grateful for a hint into the right direction or could show me an example on importing an .d.ts from an arbitrary location.

Thanks!

#

How to use types defined outside of ./node_modules/@types ?

craggy loom
#

I really dont understand what to do about this. I already have an existing .d.ts file under /src/episode.d.ts but when i create a new .d.ts next to it VSCode and ts-node can not find it

lone prawn
#

"types": ["reflect-metadata"],

#

If types is specified, only packages listed will be included in the global scope.

#

you don't want that

craggy loom
lone prawn
#

you need to import it

craggy loom
#

Also i dont get why my one d.ts works and the other doesnt

lone prawn
#

in your TS code

#

like so

#

import "reflect-metadata";

#

oh

#

wait

craggy loom
#

This is really confusing

lone prawn
#

yeah, they provide thir own types

#

just ignore what they say

#

should not be needed

lone prawn
lone prawn
craggy loom
#

I think i do not need to import the package type anymore, i just use the factory function i want to use and create a type using ReturnType<typeof myFunc> but ts-node and VSCode cant find the type defined in the second .d.ts named youtubedl.d.ts

src/
โ”œโ”€ types/
โ”‚  โ”œโ”€ episode.d.ts
โ”‚  โ”œโ”€ youtubedl.d.ts
#

Episode works fine but YouTubeDL does not

lone prawn
#

wait

#

why do you have .d.ts files in the first place?

#

do you have JS code you're tryuing to add types to?

#

or is it new code you are writing?

craggy loom
lone prawn
#

that's not what those files are for

#

just use a regular TS file, even if it only contains types

craggy loom
#

That is what i am trying to do in the latter d.ts file:

declare type YouTubeDl =  ReturnType<typeof create>;
lone prawn
#

.d.ts files add types to your JavaScript library
you don't have an existing JavaScript library

lone prawn
craggy loom
#

Hmm so i removed the types property from the tsconfig, i moved reflect-metadata into the first line of index.ts and renamed my d.ts files to .ts but it is still the same behaviour

#

I can use the Episode type perfectly but not the YouTubeDL one

lone prawn
#

what error do you have exactly?

craggy loom
#

It cant find the type

lone prawn
#

"es2019", "es6" this is a problem too

#

either set es2019 or es6, but not both

lone prawn
#

you might also want to remove "rootDir": "./src",

craggy loom
#

The following code block is the conents of src/types/youtubedl.ts

import { create } from 'youtube-dl-exec';

declare type YouTubeDL = ReturnType<typeof create>;

Now in another file i can only use the type if i remove the import but without the import i can not reference the create function so i can not use its return type.

i also can not use the type defined in the youtube-dl-exec package since ts-node and vscode do not find it no matter what i try.
https://github.com/microlinkhq/youtube-dl-exec/blob/a2971a16b32dcc9bb424effcb6ae5b0cfd256b2c/src/index.d.ts#L249

So the following also does not work at all as it can not find the YtdlCreateFuncion type:

declare type YouTubeDL = ReturnType<YtdlCreateFuncion>;
lone prawn
#

./

#
import { create } from './youtube-dl-exec';

type YouTubeDL = ReturnType<typeof create>;
#

without a leading ./, TS thinks it's a package coming from npm

#

which it isn't

craggy loom
#

It is

lone prawn
#

oh

craggy loom
#

check the link above it is the code i try to reference

#

tbh i feel a bit embarassed at myself that i am stuck for 3 hours now at a simple type...

split sigilBOT
#
Ascor8522#7606

Preview:```ts
import {create} from "youtube-dl-exec"

type YouTubeDL = ReturnType<typeof create>```

lone prawn
#

working just fine

craggy loom
#

Only when i remove the import YouTubeDL does light up and the error in the other file disappears

lone prawn
#

did you actually install the package?

craggy loom
#

Yes

lone prawn
#

what does you tsconfig look like?

craggy loom
#

checked it like 3 days now

#

Posted above minus the types as you said

lone prawn
#

try removing the "extends": "./node_modules/gts/tsconfig-google.json",

#

there is little to no reason to use that

craggy loom
#

But is that the root of the problem?

#

Like i can use the type

#

once i remove import

#

I really dont get this

#

Do i have to export the type? VSCode does want to export import it but only this type, Episode is still just fine

craggy loom
#

Alright, it does look like once i use import i have to explicitly export the type, the following does work for me:

import { create } from 'youtube-dl-exec';

declare type YouTubeDL = ReturnType<typeof create>

export default YouTubeDL;
lone prawn
#

right, if you want to use a type, you have to export it

lone prawn
#

!resolved