I'm encountering an issue that is giving me trouble. I need to share some interfaces and types across my class files.
The structure of my repository looks like this:
test
-> dist/
-> src/
-> index.ts
-> .babelrc
-> .eslintrc.js
-> index.d.ts
-> package.json
-> tsconfig.json
In index.d.ts
:
declare module test { // I've try with 'declare namespace test' but the result is the same
export interface LabeledValue {
label: string;
size: number;
}
}
When I try to import LabeledValue
in index.ts
, I receive an error that I can't comprehend:
import { LabeledValue} from 'test'; // TS2307: Cannot find module 'test'.
src/index.ts
import {LabeledValue} from 'test';
function printLabel(labeledObj: LabeledValue) {
console.log(labeledObj.label);
}
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);
package.json:
{
"name": "test",
"types": "index.d.ts",
"version": "1.0.0",
"description": "test",
"author": "oyabi",
"license": "MIT",
"private": true,
"scripts": {
"type-check": "tsc --noEmit",
"type-check:watch": "yarn type-check --watch",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir dist --extensions \".ts,.tsx\" --source-maps inline"
},
"dependencies": {
"axios": "^0.19.0",
...
},
"devDependencies": {
"@babel/cli": "^7.7.0",
...
}
}
.babelrc
{
"presets": ["@babel/typescript",
["airbnb", {
"useBuiltIns": "usage"
}]
],
"plugins": [
"syntax-class-properties",
"transform-class-properties"
]
}
.eslintrc
module.exports = {
root: true,
parser: "@typescript-eslint/parser",
...
};
tsconfig.json
{
"compilerOptions": {
"target": "esnext",
...
},
"include": ["index.d.ts"]
}
My actual code is more complex, but this should give you a good understanding of the issue. The error occurs in Visual Studio Code and with yarn build
command. You can use yarn tsc src/index.ts
to see the error.
If you have any suggestions on how to resolve this problem, please let me know.