为什么VSCODE打字稿自动完成在简单 /复杂的联盟上如此奇怪
基本上,我有一个带有2个属性的打字稿类型,我希望Vscode能够在完成第一个属性后能够自动完成 / IntelliSense。
真正奇怪的是,Vscode可以做到这一点,但是前提是工会足够复杂……但是如果它非常简单。
我认为最简单的解释方法是使用几个代码示例。
这可以按照您期望的方式工作:
type MyGenericType<a, b> = {
property1: a;
property2: b;
};
type MyUnion =
MyGenericType<"a", "a">
| MyGenericType<"b", "b">
| MyGenericType<"c", "c">
| MyGenericType<"d", "d">
| MyGenericType<"e", "e">
| MyGenericType<"f", "f">
| MyGenericType<"g", "g">
| MyGenericType<"h", "h">
| MyGenericType<"i", "i">
| MyGenericType<"j", "j">;
const myConstant: MyUnion = {
property1: "a",
property2: "", // VSCode can suggest correctly the only possible string here is "a"
};
但是,如果我们从联合中删除项目不起作用:
type MyGenericType<a, b> = {
property1: a;
property2: b;
};
type MyUnion =
MyGenericType<"a", "a">
| MyGenericType<"b", "b">
| MyGenericType<"c", "c">
| MyGenericType<"d", "d">
| MyGenericType<"e", "e">
| MyGenericType<"f", "f">
| MyGenericType<"g", "g">
| MyGenericType<"h", "h">
| MyGenericType<"i", "i">;
const myConstant: MyUnion = {
property1: "a",
property2: "", // VSCode suggests property2 might be any letter from "a" to "i" (of course, it can only be "a")
};
要清楚,打字稿(和vscode)正确识别何时错误地构造了我的constant。问题是自动完成 / intelisense。
如果有人知道这里发生了什么,或者是一个已知的错误...或实际上。那真是太神奇了。
谢谢。
Basically I have a typescript type with 2 properties, and I expect VSCode to be able to autocomplete / intellisense the second of those properties after I complete the first.
The really strange thing is that VSCode can do this, but only if the union is complicated enough ... but not if it's very simple.
I think the easiest way to explain is with a couple of code examples.
This works the way you'd expect:
type MyGenericType<a, b> = {
property1: a;
property2: b;
};
type MyUnion =
MyGenericType<"a", "a">
| MyGenericType<"b", "b">
| MyGenericType<"c", "c">
| MyGenericType<"d", "d">
| MyGenericType<"e", "e">
| MyGenericType<"f", "f">
| MyGenericType<"g", "g">
| MyGenericType<"h", "h">
| MyGenericType<"i", "i">
| MyGenericType<"j", "j">;
const myConstant: MyUnion = {
property1: "a",
property2: "", // VSCode can suggest correctly the only possible string here is "a"
};
However, if we remove an item from the union things don't work:
type MyGenericType<a, b> = {
property1: a;
property2: b;
};
type MyUnion =
MyGenericType<"a", "a">
| MyGenericType<"b", "b">
| MyGenericType<"c", "c">
| MyGenericType<"d", "d">
| MyGenericType<"e", "e">
| MyGenericType<"f", "f">
| MyGenericType<"g", "g">
| MyGenericType<"h", "h">
| MyGenericType<"i", "i">;
const myConstant: MyUnion = {
property1: "a",
property2: "", // VSCode suggests property2 might be any letter from "a" to "i" (of course, it can only be "a")
};
To be clear, typescript (and VSCode) correctly identify when myConstant is constructed incorrectly. The issue is the autocomplete / intelisense.
If anyone has any idea what is going on here, or if it's a known bug ... or anything actually. That would be amazing.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论