Skip to content

Scoped module type definition files are not resolved #15204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bentruyman opened this issue Apr 15, 2017 · 3 comments
Closed

Scoped module type definition files are not resolved #15204

bentruyman opened this issue Apr 15, 2017 · 3 comments
Labels
Fixed A PR has been merged for this issue

Comments

@bentruyman
Copy link

TypeScript Version: 2.2.2

Code

// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "dist",
    "target": "es5",
    "typeRoots": ["types"]
  },
  "include": ["src/**/*"]
}
// src/index.ts
import * as scoped from "@scope/scoped";

scoped("str");
// node_modules/@scope/scoped
module.exports = function (str) {
    return str;
}
// types/@scope/scoped/index.d.ts
declare module "@scope/scoped" {
    interface Scoped {
        (str: string): string;
    }

    const scoped: Scoped;

    export = scoped;
}

Expected behavior:

Running tsc should successfully compile the project into the dist directory.

Actual behavior:

A compile error is printed:

error TS2688: Cannot find type definition file for '@scope'.

Comments:

This is because it appears the compiler is only looking in the top-level directory (in this case, types/@scope) for a type definition and not the directory of the scoped package (types/@scope/scoped). A few projects in the DefinitelyTyped project have gotten around this limitation by employing some variations of the same technique[1][2][3].

Here's what I've seen as workarounds:

  • Publish an entire scope's module definitions under a single file @scope/* => scope (downloading more than necessary)
  • Publish a scoped module as un-scoped @scope/module => module (collisions possible?)
  • Use a special naming convention @scope/module => scope__module

In this project, the issue can be fixed if one just moves types/@scope/scoped to types/scoped.

Maybe this is something that just needs to be handled in conventions used by the folks in the DefinitelyTyped project (e.g. @scope/module => scope__module). But what this issue is proposing is a solution that is likely to be the expected experience when doing the above. This is maybe less of a bug and more of a developer ergonomics/UX issue.

I'd be perfectly cool closing this issue and just recommending documentation changes be made to call out this case and suggest the third bullet item above as a solution. This use case is especially useful for those of us individuals or companies with a semi-large catalog of scoped private packages and are making the migration to Typescript.

@ghost
Copy link

ghost commented Apr 15, 2017

I have a PR out for your third option there. #15051

@blakeembrey
Copy link
Contributor

See also: microsoft/types-publisher#155.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Apr 17, 2017
@mhegazy mhegazy added this to the TypeScript 2.3.1 milestone Apr 17, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Apr 17, 2017

Should be addressed by #15051

@mhegazy mhegazy closed this as completed Apr 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants