You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This doesn't work (MyEnum imported from separate file):
// my-enum.js/** @enum {number} */constMyEnum={a: 1,b: 2};exportdefaultMyEnum;// main.jsimportMyEnumfrom'./my-enum.js';/** @type {MyEnum} */// ERROR: 'MyEnum' refers to a value, but is being used as a type here. ts(2749)constv=MyEnum.b;
I have the inverse of this issue, where I can't do something like:
import{MyEnum}from'./my-enum.js';constfoo=MyEnum.a;// "'MyEnum' only refers to a type, but is being used as a value here"constbar=MyEnum['b'];// Same thingswitch(baz){caseMyEnum.a: // and also hereconsole.log("xyz");break;caseMyEnum.b: //and herebreak;default:
}
It used to work as of TypeScript 3.5.3, I discovered this issue when upgrading a project this morning.
#34515 appears to improve the situation -- it resolves a type -- but it resolves the wrong type. MyEnum resolves to { [s: string]: number } not number as it should be.
The problem is that the imported MyEnum is marked as BlockScopedVariable, not TypeAlias. So it incorrectly falls through the type alias case and uses the jsdoc code path that treats values as types. This is not the right code path (I think) even though it now returns a type.
I think the fix is to resolve the alias symbol for MyEnum correctly.
I believe Eli Barzilay (@elibarzilay) is looking at the alias resolution code right now in order to fix the symbol's flags. I hope it's an overlooked code path and not something deep in alias resolution.
Thanks everyone for the great work recently on JSDoc support, which allows us to publish auto-generated .d.ts files from our JSDoc annotated JavaScript code base. That's awesome!
This one is the last blocker for TypeScript being 100% useful with JSDoc annotations. Is there a new milestone for this? It is still broken in v3.9.0-dev.
I believe this is because MyEnum is also used as an object literal here, so the type is resolved differently. The above is equivalent to this, which triggers the same error:
Activity
nickpelone commentedon Sep 24, 2019
I have the inverse of this issue, where I can't do something like:
It used to work as of TypeScript 3.5.3, I discovered this issue when upgrading a project this morning.
ahocevar commentedon Sep 25, 2019
I can confirm that 3.5.3 was the last version where Sergey Gavrilov (@art-in)'s scenario worked. We are having the same issue in OpenLayers.
What still works is this:
But that's a bit cumbersome, considering that we have MyEnum imported already.
wulfsberg commentedon Sep 27, 2019
Given that 3.7 has the game-changing #7546, I would be very sad to not be able to use it because of this.
sandersn commentedon Oct 16, 2019
#34515 appears to improve the situation -- it resolves a type -- but it resolves the wrong type.
MyEnumresolves to{ [s: string]: number }notnumberas it should be.The problem is that the imported
MyEnumis marked asBlockScopedVariable, notTypeAlias. So it incorrectly falls through the type alias case and uses the jsdoc code path that treats values as types. This is not the right code path (I think) even though it now returns a type.I think the fix is to resolve the alias symbol for
MyEnumcorrectly.ahocevar commentedon Nov 26, 2019
I can confirm that #34515 does not fix the issue. The error is now
Nathan Shively-Sanders (@sandersn) Is there anything I can help with to get this resovled?
sandersn commentedon Dec 5, 2019
I believe Eli Barzilay (@elibarzilay) is looking at the alias resolution code right now in order to fix the symbol's flags. I hope it's an overlooked code path and not something deep in alias resolution.
ahocevar commentedon Feb 17, 2020
I just tried v3.8.1-rc, but this is still broken. Has there been any progress?
ahocevar commentedon Mar 11, 2020
Thanks everyone for the great work recently on JSDoc support, which allows us to publish auto-generated .d.ts files from our JSDoc annotated JavaScript code base. That's awesome!
This one is the last blocker for TypeScript being 100% useful with JSDoc annotations. Is there a new milestone for this? It is still broken in v3.9.0-dev.
ahocevar commentedon Mar 11, 2020
The problem boils down to this:
Assume you have the following
my-enum.jsfile:Now when you import the enum in a different file, the following will work:
The follwoing will cause the reported error:
I believe this is because
MyEnumis also used as an object literal here, so the type is resolved differently. The above is equivalent to this, which triggers the same error:9 remaining items
elibarzilay commentedon Aug 3, 2020
Quick note: the
/** @type {import("./my-enum.js").default} */case is broken too -- it gets a type ofany.ahocevar commentedon Aug 4, 2020
And what Eli Barzilay (@elibarzilay) mentions also happens when the workaround mentioned in #33575 (comment) is used.
elibarzilay commentedon Aug 4, 2020
Nathan Shively-Sanders (@sandersn): I just tried #39770, and it doesn't fix this issue or the
anythat I mentioned above.Avoid the double-symbol trick for enums
Avoid the double symbol for names that are explicitly exported
Avoid the double-symbol trick for enums
Avoid the double symbol for names that are explicitly exported
Avoid the double-symbol trick for enums
Avoid the double-symbol trick for enums
elibarzilay commentedon Aug 11, 2020
Quick note²: The
/** @type {import("./my-enum.js").default} */thing is working with the fix too.Avoid the double-symbol trick for enums