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.
TypeScript Version: 2.2.2
Code
Expected behavior:
Running
tscshould successfully compile the project into thedistdirectory.Actual behavior:
A compile error is printed:
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:
@scope/* => scope(downloading more than necessary)@scope/module => module(collisions possible?)@scope/module => scope__moduleIn this project, the issue can be fixed if one just moves
types/@scope/scopedtotypes/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.