为什么VSCODE打字稿自动完成在简单 /复杂的联盟上如此奇怪

发布于 2025-02-05 08:14:02 字数 1459 浏览 1 评论 0原文

基本上,我有一个带有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文