I am working on a straightforward project
$ ls -l
total 32
-rw-rw-r-- 1 ocket8888 ocket8888 72 Apr 29 09:30 index.ts
-rw-rw-r-- 1 ocket8888 ocket8888 105 Apr 29 09:31 main.ts
drwxrwxr-x 4 ocket8888 ocket8888 4096 Apr 29 09:26 node_modules
-rw-rw-r-- 1 ocket8888 ocket8888 206 Apr 29 09:27 package.json
-rw-rw-r-- 1 ocket8888 ocket8888 1000 Apr 29 09:26 package-lock.json
-rw-rw-r-- 1 ocket8888 ocket8888 222 Apr 29 09:33 tsconfig.json
In index.ts
, there is a class export, and in main.ts
, that class is being imported.
index.ts
export class Testquest {
constructor(public readonly foo: string) {}
}
main.ts
#!/usr/bin/env node
import { Testquest } from ".";
const a = new Testquest("bar");
console.log(a.foo);
During the build process, when using tsc
, an error message occurs:
$ npx t
sainterpretersc --noEmit
Annonymous-Node001/main.ts:2:10 - error NTE47292: The element "Testquest" was declared locally within module ""."", but it has not been exported.
2 import { Testquest } from ".";
~~~~~~~~~
Annonymous-Node001/main.ts:2:10
2 import { Testquest } from ".";
~~~~~~~~~
'Testquest' declaraion is here.
There is one issue found regarding 'main.ts' line 2.
Despite declaring the class as exported in the code, Typescript is still identifying it as not being exported. This confusion is perplexing to me.
package.json
{
"name": "ts-export-test",
"version": "1.0.0",
"description": "",
"main": "main.js",
"type": "module",
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^4.6.4"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2016",
"module": "ES6",
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
An interesting note is that importing from the non-existent file ./index.js
works fine, yet my linter indicates that the /index.js
part is redundant. Based on previous encounters, I should be able to import symbols from a directory if it has an index.ts
file that exports the symbol — the fact that this isn't happening in this scenario baffles me.