#typescript is about to make me cry - trying to import a module

23 messages · Page 1 of 1 (latest)

arctic oasis
#

I wrote a module in typescript. I'm trying to import that module into another module also written in typescript. I naively thought this would be simple and intuitive.

$ npx tsc --build tsconfig.json
error TS2688: Cannot find type definition file for 'cc-typescript'.
  The file is in the program because:
    Entry point for implicit type library 'cc-typescript'


Found 1 error.

This is the import in my index.ts that is failing:

import { Model } from '@ccorg/cc-typescript';

The @ccorg/cc-typescript module itself exports its types in this way, and I've verified that the specified index.d.ts file does exist:

  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",

Any assistance is greatly appreciated. It feels awful to be stuck on importing a module; I just want to move past this and work on my project.

subtle epoch
#

everything looks alright to me MeowThink

arctic oasis
#

could it be because I installed it from a private registry? All the files seem to be in the right places though

subtle epoch
#

as long as the package is present in your node_modules you should be good

arctic oasis
#

I don't see declare module '@ccorg/cc-typescript in my index.d.ts inside of @ccorg/cc-typescript, I only mention that because the error message suggests adding this; is that necessary inside of index.d.ts? (It just has all the type in it; I don't see a declare module line)

subtle epoch
#

it should not be necessary

arctic oasis
#

Maybe I did something wrong in tsconfig.json

{
    "$schema": "https://json.schemastore.org/tsconfig",
    "compilerOptions": {
        "lib": ["esnext", "dom"],
        "moduleResolution": "node",
        "strict": true,
        "noUncheckedIndexedAccess": true,
        "inlineSources": true,
        "sourceMap": true,
        "composite": true,
        "rootDir": "src",
        "outDir": "dist"
    }
}
subtle epoch
#

tsc tries to look up the package in the node_modules folder and assumes all types found are for this package by default

arctic oasis
subtle epoch
#

declare module '@ccorg/cc-typescript is only needed if the current file is not part of the @ccorg/cc-typescript module

arctic oasis
#

The tsconfig.json for @ccorg/cc-typescript looks like this, very similar to the one in the module I'm importing from:

{
    "$schema": "https://json.schemastore.org/tsconfig",
    "compilerOptions": {
        "lib": ["esnext", "dom"],
        "strict": true,
        "noUncheckedIndexedAccess": true,
        "inlineSources": true,
        "sourceMap": true,
        "composite": true,
        "rootDir": "src",
        "outDir": "dist"
    }
}
#

Oh okay, index.d.ts is actually missing from inside node_modules. I have no idea why I thought it was there (I swear I checked with vim), but now I'm left still having no clue why it's missing.

#

Yeah okay, dist/ only existed under the node_modules directory because of a npm link <modulename> which I did earlier. Apparently npm unlink doesn't entirely clean things up.

So now I need to figure out how to get dist/ in there. Google is broken, so I'm really struggling to figure that out.

#

"scripts": { "prepare": ... } was suggested but this doesn't work

arctic oasis
#

I found a really elegant solution to my problem. Here's what I did:

  • deleted all my tsconfig.json files
  • renamed all the .ts files to .js
  • removed all the types
  • changed each package.json to point to the js file under src/ instead of the one in dist/
  • I ran npm remove typescript

Now everything works great. I've sacrificed type-safety, but I tend to use good abstractions and I always know what type of value I'm passing to a function I'm calling (and what type of value it expects), so this isn't much of an issue for me.

subtle epoch
#

you can always use JSDoc if needed

#

but imo the problem was more something that had to do with your local development environment

arctic oasis
#

I spent a week developing a monorepo with typescript modules and I actually got pretty far. I had a registry of heterogenous types that was able to determine the type of the value I was getting from a key, and it was starting to look really flexible as I was getting past limitations that had previously prevented me from using typescript. However then, upon moving to a private npm registry, it was like I was back at square one. It's really difficult for me to justify using something that slows me down so much for type safety. I work on a large vanilla js project and we've only ever had two or three prod errors related to type errors over the past 2 years.

subtle epoch
#

the registry you are getting your packages from should have no impact
since type-safety is checked once the package is installed in your project's node_modules folder, no matter the source

#

but tbh, it's hard to guarantee everything is right in your code without actually seeing the typings, the package.json, the folder structure, etc.

#

¯_(ツ)_/¯