Reputation: 1102
Having two modules as follows gives the related error (simplified in edit):
// b.ts
export const sy = Symbol();
export const p = { [sy]: 0 };
// index.ts
import { sy, p } from './b';
export const a: { [sy]: number } = p; // works
export const b = p; // error
Exported variable 'b' has or is using name 'sy' from external module "c:/my/cool/path/b" but cannot be named.ts(4023)
(with declaration
enabled)
Note, that the error disappears, when the inferred type is explicitly copied into the annotation. When changing to export const sy = 'someString';
, it works as well (limited to symbols). Even more weird, the error disappears, when transforming the symbol export to:
const sy = Symbol();
export { sy };
(splitting the export off into its own line)
I can't understand at all, what the problem even is here, and the last part confuses me even more, as both representations should be equivalent, but aren't.
Is this a bug? I can switch to non-symbols, but besides semantic differences, that means giving up, and accepting "symbols just don't work", which isn't really great.
I am currently assuming it's related to https://github.com/microsoft/TypeScript/issues/40718#issuecomment-738295483, a bug in the declaration generator. Some kind of confirmation would be useful though.
Upvotes: 2
Views: 884
Reputation: 113
This is in fact already explained by the issue you pointed:
Declarations within a module are represented by two symbols, a local symbol and an export symbol.
p
in b.ts points to the local symbol of sy.
Where the ts compiler has a bug, is when b now looks up the local symbol of sy
in b.ts:
It traverses the exports of b.ts via aliases to find the symbol, but ends up with the declaration which is not equal to the local symbol.
This turn makes the compiler think symbol isn't visible, when, in fact, it is (because of the localSymbol/exportSymbol relationship).
Upvotes: 1
Reputation: 5717
it looks like your code example works just fine, tasted on :
node v14.15.4
typescript": "~4.2.2"
macOS Catalina 10.15.7
and this tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"allowJs": true,
"importHelpers": true,
"jsx": "react",
"alwaysStrict": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": false,
"noImplicitThis": false,
"strictNullChecks": false
},
"include": [
"src/**/*",
"__tests__/**/*"
]
}
possible windows related issue
here is a full working project setup example
https://github.com/ntedgi/playground
Upvotes: 0