0

This might just be wishful thinking:

I have a module foo that uses the Parameters<> from a different mod bar, and passes it out of the current mod (foo). So now if some new mod baz requires foo, it has to Also install the @types for the mod that foo is using: bar in order to figure out the Parameters<> Type. This feels like a common situation, and I was hoping there was a way to handle it that didn't involve forcing baz to need all the declarations from bar, like a way that the extracted types compiled into foo's declaration maybe. Greatly appreciate any guidance!

module bar index.ts:

export function doSomething(param: any) { }

module foo index.ts:

import { doSomething } from "bar"
export function DoAnotherThing(param: Parameters<typeof doSomething>[0]) {
    doSomething(param)
}

module baz index.ts

import { doAnotherThing } from "foo"

// if @types/bar isn't also installed:
// Type 'unknown' has no matching index signature for type 'number'
doAnotherThing(123)

1 Answer 1

1

Let the dependency tree just do its work. You don't need any special magic here.

You should just need to add @types/bar in the devDependencies of the foo module. Now npm install foo should install foo, bar and @types/bar, and everything should work just fine.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the response! That's surprising and very helpful to know that this should be working. Maybe my setup is what's causing the problem then: I'm pulling modules and types from my own github repo, ex: "@mytypes/foo" and "@myapps/foo", and I have to manually add the "@mytypes" directory to typeRoots in every mod's tsconfig. And the tsconfig doesn't exist for imported modules, so it probably can't find the declaration? Would including the tsconfig in the package fix it? Pretty lost on this one.
--edit-- my type declarations have their own/separate empty package.json for the "@mytypes" package, so there's no way to follow the dependency tree. I need to find a different way to split up the JS files from the D.TS files than using two package.json files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.